tags:

views:

26

answers:

2

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.';
    }
+2  A: 

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.

R. Hill
How do I test for the return value?
lone
See edits in my comment
R. Hill
+1  A: 

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.';
  }
}
jordanstephens