Tutorial :PHP & MySQL - Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given [duplicate]



Question:

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

I was wondering how can correct the error I keep0 getting listed below.

I get the following error.

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given on line 159  

Line 159 is

$foundnum = mysqli_num_rows($run);  

Here is part of the PHP & MySQL code.

    mysqli_select_db($mysqli, "sitename");            $search_explode = explode(" ", $search);            foreach($search_explode as $search_each) {              $x++;              if($x == 1){                  $construct .= "(users_comments.article_content LIKE '%$search_each%' OR users_comments.title LIKE '%$search_each%' OR users_comments.summary LIKE '%$search_each%')";              } else {                  $construct .= "(OR users_comments.article_content LIKE '%$search_each%' OR users_comments.title LIKE '%$search_each%' OR users_comments.summary LIKE '%$search_each%')";              }            }            $construct = "SELECT users.*, users_comments.* FROM users INNER JOIN users_comments ON users.user_id = users_comments.user_id WHERE $construct ORDER BY users_comments.date_created DESC";          $run =  mysqli_query($mysqli, $construct);            $foundnum = mysqli_num_rows($run);        if ($foundnum == 0) {          echo 'No results found.';      }  


Solution:1

You have to test $run to make sure mysqli_query() didnt return false.

if ($run != false) {    $foundnum = mysqli_num_rows($run);    if ($foundnum == 0) {      echo 'No results found.';    }  }  


Solution:2

It's because

if ( $result = mysqli_query($mysqli, $construct) ) {      // got something!      }  

returned FALSE. You have to test for the return value before using it as a result set. You must have an error in your SQL statement.

Small advice: To avoid repeating the complex SQL string in your loop, use a $prefix variable, like so:

$prefix = '';  foreach($search_explode as $search_each) {      $construct .=  "$prefix (users_comments.article_content LIKE '%$search_each%' OR users_comments.title LIKE '%$search_each%' OR users_comments.summary LIKE '%$search_each%') ";      $prefix = ' OR' ;  }  

I suspect the error is arount there anyway, you put your 'OR' inside the parenthesis instead of outside.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »