0

I have a query which perform fine, but the problem is somehow its gives me the warning when no data match from database.

$count = mysql_num_rows($qry); //warning here

I can use @ to hide the error, but this is not good programming as far i know, so how can i hide the warning from the output?

The Warning: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in [somepage.php] on line [11]

$count = @mysql_num_rows($qry); //No warning

So i know there will be the ways to hide the warning, but i don't know. please help me on this.

1 Answers1

2

Some folks are saying you should work around this issue by suppressing warnings. Don't listen to them -- warnings are telling you that something is wrong. Suppressing them defeats their entire purpose.

I'm also assuming you know that the mysql_ functions are deprecated in favor of mysqli. Others have mentioned this and they're absolutely right -- there are a bunch of security issues with the mysql extension. It's so bad that if you want to upgrade to PHP 7 you'll be forced to use mysqli because mysql has been completely removed.

But in general, when something returns a resource on success and false on failure, you can check for failure before using the resource. Like so:

$rslt = mysql_query($query);
if (!$rslt)
{
    // handle error
}
else
{
    $count = mysql_num_rows($rslt);
    // ...
}

As an alternative to checking result as a boolean, you could swap the cases and use is_resource.

Zach Rattner
  • 20,745
  • 9
  • 59
  • 82