开发者

PHP/MySQL: What is returned when no matches are found?

开发者 https://www.devze.com 2023-03-19 12:20 出处:网络
I want to use PHP to find out if no matches are found. I tried the following, but \"is_resource()\" always returns true.

I want to use PHP to find out if no matches are found. I tried the following, but "is_resource()" always returns true.

$result = mysql_query('...');
if(is_resource($re开发者_如何学JAVAsult)){
// Results are found
}


mysql_num_rows() will do the trick.

if (mysql_num_rows($result)>0) {
    //Results are found
}

http://php.net/manual/en/function.mysql-num-rows.php


So $result will always be a resource as long as you have proper access to the database. And mysql_num_rows() assumes that the query itself ran successfully. I'd say try something like this:

if($result === FALSE) // Query failed due to not having proper permissions on the table
    die('Invalid query: ' . mysql_error());
else if(mysql_num_rows($result) >0)) // We have more than 1 row returned which means we have data
    // INPUT RESULTS PROCESSING HERE
else // No rows were returned therefore there were no matches
    echo 'No rows returned';

Hope that helps a little =) Look here for more information if you need: http://www.php.net/manual/en/function.mysql-query.php


This is what you want: mysql_num_rows()


If the query fails it mysql_query will return false so you can check your code like this:

if ( $stmt = mysql_query("...") )
{
    // Do some things
}
else
{
    // Do some other things
}

Or you could use mysql_num_rows like the people above have stated. But you should really be looking into MySQLi it's a built in database class. Learn it and use it. Your life will be so much easier.

0

精彩评论

暂无评论...
验证码 换一张
取 消