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

发布于 2024-11-19 10:12:28 字数 169 浏览 0 评论 0原文

我想使用 PHP 来查找是否没有找到匹配项。我尝试了以下操作,但“is_resource()”始终返回 true。

$result = mysql_query('...');
if(is_resource($result)){
// Results are found
}

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($result)){
// Results are found
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

神也荒唐 2024-11-26 10:12:28

mysql_num_rows() 就可以了。

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

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

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

靖瑶 2024-11-26 10:12:28

因此,只要您能够正确访问数据库,$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';

希望有一点帮助=)
如果需要,请在此处查看更多信息:http://www.php。 net/manual/en/function.mysql-query.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

饮湿 2024-11-26 10:12:28

这就是你想要的: mysql_num_rows()

This is what you want: mysql_num_rows()

Spring初心 2024-11-26 10:12:28

如果查询失败,mysql_query 将返回 false,因此您可以像这样检查代码:

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

或者您可以像上面的人所述使用 mysql_num_rows 。
但你真的应该研究 MySQLi 它是一个内置的数据库类。学习它并使用它。你的生活将会变得更加轻松。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文