Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP/MySQL:没有找到匹配项时返回什么?_Php_Mysql_Sql - Fatal编程技术网

PHP/MySQL:没有找到匹配项时返回什么?

PHP/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始终

我想使用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始终是一个资源。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_行。 但您应该真正了解它是一个内置的数据库类。学习并使用它。你的生活会轻松得多