Burning Hippo
Burning Hippo

Reputation: 805

mysqli_query() not producing expected results

I have a function being called:

function getUnitID($unit_name) {
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
echo '<br/>'.$query = ' SELECT id, unit_name FROM units WHERE unit_name LIKE "'.trim($unit_name).'"'.'</br>';
$result = mysqli_query($con, $query);
$id = '';
echo 'here';
while ($row = mysqli_fetch_assoc($result)) {
    echo 'yup';
    $id = $row['unit_name'];
    if (strlen($id) > 0) 
        echo 'nothing';
    else echo 'something!';
    var_dump($row);
}
mysqli_close($con);
return $id;
}

My output is:

SELECT id, unit_name FROM units WHERE unit_name LIKE "a name here"
here

However, when I run it in my database I get a row back.

Notice the echo 'yup'; line doesn't output anything. I assume something with $result = mysqli_query($con, $query); is messed up, but I know the $con is fine because it works in other places.

Upvotes: 1

Views: 54

Answers (1)

Radu Bogdan
Radu Bogdan

Reputation: 504

I think the problem is here:

echo '<br/>'.$query = ' SELECT id, unit_name FROM units WHERE unit_name LIKE "'.trim($unit_name).'"'.'<br />';

Make it like this because i'm pretty sure you mess up the $query:

$query=' SELECT id, unit_name FROM units WHERE unit_name LIKE "'.trim($unit_name).'"';
echo '<br/>'.$query.'<br />';

I'm not sure what you are looking for exactly to select but i have another hunch that you want to do this:

$query=' SELECT id, unit_name FROM units WHERE unit_name LIKE "%'.trim($unit_name).'%"';
echo '<br/>'.$query.'<br />';

Upvotes: 3

Related Questions