PHP/MySQL:没有找到匹配项时返回什么?
我想使用PHP来找出是否没有找到匹配项。我尝试了以下操作,但“is_resource()”始终返回truePHP/MySQL:没有找到匹配项时返回什么?,php,mysql,sql,Php,Mysql,Sql,我想使用PHP来找出是否没有找到匹配项。我尝试了以下操作,但“is_resource()”始终返回true $result = mysql_query('...'); if(is_resource($result)){ // Results are found } mysql\u num\u rows() if (mysql_num_rows($result)>0) { //Results are found } 这就是您想要的:所以只要您能够正确访问数据库,$result始终
$result = mysql_query('...');
if(is_resource($result)){
// Results are found
}
mysql\u num\u rows()
if (mysql_num_rows($result)>0) {
//Results are found
}
这就是您想要的:所以只要您能够正确访问数据库,$result始终是一个资源。mysql_num_rows()假设查询本身已成功运行。我想说试试这样的东西:
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';
if ( $stmt = mysql_query("...") )
{
// Do some things
}
else
{
// Do some other things
}
希望这有点帮助()
如果需要,请查看此处了解更多信息:如果查询失败,mysql\u查询将返回false,因此您可以像这样检查代码:
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';
if ( $stmt = mysql_query("...") )
{
// Do some things
}
else
{
// Do some other things
}
或者您可以像上面提到的那样使用mysql_num_行。
但您应该真正了解它是一个内置的数据库类。学习并使用它。你的生活会轻松得多