bonny
bonny

Reputation: 3247

updating while loop to mysqli

hello everyone i have the following issue to solve. i have an autosuggest search field. i would like to update my code from using mysql to mysqli. thats why i have this code ($db=mysqli):

if (isset($_POST['search_term']) == true && empty($_POST['search_term']) == false) {

    $search_term = $db->real_escape_string(htmlentities(trim($_POST['search_term'])));

    $search_term_query = "SELECT `a` FROM `b` LIKE '$search_term%'";
    $result_search_query = $db->query($search_term_query);

    while (($row = $result_search_query->fetch_assoc()) !== false) {

        echo '<li>', $row['a'], '</li>';
    }

}

for some reason i get:

Call to a member function fetch_assoc() on a non-object

so where is the problem with the object? if there is someone who could help me out i really would appreciate. thanks a lot.

Upvotes: 1

Views: 206

Answers (1)

rationalboss
rationalboss

Reputation: 5389

This means that $result_search_query is not the object you are expecting. Possible reasons:

  1. connection failed to the database
  2. query execution failed

Please take a look at the examples in http://php.net/manual/en/mysqli-result.fetch-assoc.php and http://www.php.net/manual/en/mysqli.query.php. It should show you how you can catch errors.

Upvotes: 1

Related Questions