3

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();
Dharman
  • 30,962
  • 25
  • 85
  • 135
will_macomber
  • 41
  • 1
  • 1
  • 4
  • Assuming `$db` is a PDO object and the result of the query method returned false? Hence trying to access num_rows on a boolean? [PDO::query](https://www.php.net/manual/en/pdo.query.php) – Remy Jan 01 '21 at 18:33
  • 1
    @Remy - `num_rows` and `fetch_assoc` suggests it's mysqli, but your comment is just as valid for mysqli as well. – M. Eriksson Jan 01 '21 at 18:39
  • 2
    Does this answer your question? [mysql\_fetch\_array()/mysql\_fetch\_assoc()/mysql\_fetch\_row()/mysql\_num\_rows etc... expects parameter 1 to be resource](https://stackoverflow.com/questions/2973202/mysql-fetch-array-mysql-fetch-assoc-mysql-fetch-row-mysql-num-rows-etc) - Even if the error message is slightly different, it's the exact same problem, as you will see in the accepted answer. – M. Eriksson Jan 01 '21 at 18:47
  • If the `query` method returned a Boolean type then it’s likely it returned `false` due to failing. There’s an example of getting the error message on https://www.w3schools.com/php/func_mysqli_error.asp – Andrew Sayer Jan 01 '21 at 18:49
  • Thank you Javier, that did the trick! – will_macomber Jan 01 '21 at 19:14

1 Answers1

3

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

Javier Larroulet
  • 3,047
  • 3
  • 13
  • 30
  • 4
    The last sentence isn't entirely correct. If the query is valid and just can't match any records, it should still return a mysqli response object, not false. False is returned when the query can't be executed properly, like having invalid syntax, referencing non-existing columns etc. – M. Eriksson Jan 01 '21 at 18:59
  • 3
    This is not the solution to the problem. You are just sweeping the problem under a rug. Enable error reporting to see what is the problem. [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Jan 01 '21 at 21:15