Changing a SQL statement from LIKE to = - and make it work

Hi,

I have this data_SQL file:
<?php 

require_once('classes_soege.php');
require_once('soege_data_connection.php');

$data = array();

if (empty($_GET)) { return; }

$where = isset($_GET['amt']) ? "AND Amt = ?" : "";

$sql = <<<EOT
SELECT *
FROM soege_db_lokale
WHERE (Sogn LIKE ? OR Sogn LIKE ?)
$where
EOT;

if ($stmt = $mysqli->prepare($sql)):

    $terms = cityVariations(trim($_GET['sogn'])); 
    array_walk($terms, function (&$sogn) { $sogn = '%' . $sogn . '%'; });

 if (isset($_GET['amt'])):
    $stmt->bind_param('sss', $terms[0], $terms[1], $_GET['amt']);
else:
    $stmt->bind_param('ss', $terms[0], $terms[1]);
endif;

    $stmt->execute();

    $results = $stmt->get_result();

    while ($row = $results->fetch_object('LokaleTable')): // loop through the Db results

        $data[] = $row;

    endwhile;

endif;
?>

Open in new window


I need to change the LIKE to = in the WHERE clause for Sogn in the query. At the same time I don't need the cityVariations in this query.
How do I change that :)
Peter KromanSenior Proposal SpecialistAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
Hey Peter,

Change your query to the following:

$sql = <<<EOT
SELECT *
FROM soege_db_lokale
WHERE Sogn = ?
$where
EOT;

Open in new window

And then the rest of your code to something like this:

if ($stmt = $mysqli->prepare($sql)):

    if (isset($_GET['amt'])):
        $stmt->bind_param('ss', $_GET['sogn'], $_GET['amt']);
    else:
        $stmt->bind_param('s', $_GET['sogn']);
    endif;

    $stmt->execute();

    $results = $stmt->get_result();

    while ($row = $results->fetch_object('LokaleTable')): // loop through the Db results

        $data[] = $row;

    endwhile;

endif

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Peter KromanSenior Proposal SpecialistAuthor Commented:
Thanks again Chris,

It just works perfectly :)

Have a beautiful evening.
0
rgranlundCommented:
<?php

require_once('classes_soege.php');
require_once('soege_data_connection.php');

$data = array();

if (empty($_GET)) { return; }

$where = isset($_GET['amt']) ? "AND Amt = ?" : "";

$sql = <<<EOT
SELECT LIST THE TABLE NAMES THAT YOU WANT INFO FROM
FROM soege_db_lokale
WHERE Sogn='$where'
EOT;

if ($stmt = $mysqli->prepare($sql)):

    $terms = cityVariations(trim($_GET['sogn']));
    array_walk($terms, function (&$sogn) { $sogn = '%' . $sogn . '%'; });

 if (isset($_GET['amt'])):
    $stmt->bind_param('sss', $terms[0], $terms[1], $_GET['amt']);
else:
    $stmt->bind_param('ss', $terms[0], $terms[1]);
endif;

    $stmt->execute();

    $results = $stmt->get_result();

    while ($row = $results->fetch_object('LokaleTable')): // loop through the Db results

        $data[] = $row;

    endwhile;

endif;
?>
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.