Warning: Attempt to read property "num_rows" on bool in C:\xampp\htdocs\Nice Guys\index.php on line 59

Viewed 26156

So, I tried to see if this question had been asked before on here, but none of the questions dealt with non-object errors with a bool error. I'll add the line of code that is causing the error, but please let me know if you need more code for further context, and I will edit the post and add it.

I am basically trying to return search results, and I have a conditional if statement telling the browser to return search results if the results return more rows than 0. I return this error, and I am completely unsure as to why, or what is causing it.


    $searchSQL = 'SELECT * FROM images WHERE MATCH (appname, firstname, lastname, image) AGAINST ( "' . 
    $search . '" ) LIMIT ' . $offset . ', 10;';

    $searchResult = $db->query($searchSQL);

    //declade the array variable to store the results
    $output = array();


    if ($searchResult->num_rows > 0)
    {
        while ($row = $result->fetch_assoc() )
        {
            //add row to output array in the form of an associative array 
            $output[] = array ("image" => $row["image"], "firstname" => $row["firstname"], "lastname" => 
            $row["lastname"], "appname" => $row["appname"]);
        }//end while
    }//end if

    $db->close();
1 Answers

Try

if ($searchResult !== false && $searchResult->num_rows > 0)

This will avoid encountering the missing property on a boolean error in case mysqli returns false

You could even check that $searchResult is not false (or is not boolean) and not worry about the number of rows returned in the if clause, since it'll always be false when 0 rows are returned

Related