简体   繁体   中英

Mysqli - check if no result set

In reference to Check if no result found in mysql db

It was previously thought that you could check to see if there as a record set returned by doing the following;

$result= mysqli_query($conn,$sql);
$rowCount = mysqli_num_rows($result);

In the question about this was the accepted solution. While this may work fine, in the version of PHP which I'm using [PHP 5.5.12-2+deb.sury.org~precise+1] using this type of check puts an entry in my error-log

 PHP Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, 
                 boolean given in /var/www/scripts/common_php.php on line 20 

I am trying to be hyper vigilant in my code so that there are NO entries produced in my error log other than 404 errors. I am trying to code to the strictest standard. So my question remains, how do you properly determine if the record set that comes back from a query is empty or not?

Is it as simple as

 if (!result) {
    // didn't work, do something else 
 } else {
    // records set returned, resume normal processing of query result here
    //enter code here
}

** Sorry corrected the query and num_rows check lines.

It doesn't look like you got zero results - it looks like your query failed (and therefore didn't return a mysqli_result).

EDIT: To answer your question more specifically, yes it is as simple as

if($result)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM