0

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.

Community
  • 1
  • 1
Michael Fever
  • 767
  • 2
  • 7
  • 24
  • Can you wrote what is in `$rows`? – Ing. Michal Hudak Jun 02 '14 at 22:57
  • 1
    Does this answer your question? [mysqli\_fetch\_assoc() expects parameter / Call to a member function bind\_param() errors. How to get the actual mysql error and fix it?](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param) – Dharman Dec 03 '20 at 16:58
  • That link is some good info! I didn't know you could transfer a mysqli error to a php exception. From an error tracking standpoint this is pretty cool. – Michael Fever Feb 05 '21 at 15:21

1 Answers1

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)
Ford Filer
  • 350
  • 1
  • 10