Mysqli - check if no result set

半世苍凉 提交于 2020-12-27 07:14:44

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/24004551/mysqli-check-if-no-result-set

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!