如何检查从 SQL/PHP 查询返回的值是否为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8417192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:38:56  来源:igfitidea点击:

How would I check if values returned from an SQL/PHP query are empty?

phpmysql

提问by Naeem Ul Wahhab

How would I check to see if the values returned from an sql query are empty? I tried using if(empty($var)), but that didnt work either. heres my code

我将如何检查从 sql 查询返回的值是否为空?我尝试使用 if(empty($var)),但这也不起作用。继承人我的代码

PHP Code:

PHP代码:

        while($row = mysql_fetch_array($Result)) 
        { 
            if(count($row) == 0) 
            { 
                .... 
            } 
       } 

How would i check to see if $row is empty?

我将如何检查 $row 是否为空?

回答by Naeem Ul Wahhab

This is the right way to do this, you can check if the results are empty by finding number of row in returned results. There is a function in sql to do this mysql_num_rows. Here is the method to use it:

这是正确的方法,您可以通过在返回的结果中查找行数来检查结果是否为空。sql 中有一个函数可以做到这一点mysql_num_rows。这是使用它的方法:

if (mysql_num_rows($Result) == 0) { 
   //results are empty, do something here 
} else { 
   while($admin_row = mysql_fetch_array($Result)) { 
      //processing when you have some data 
}  

回答by Sudhir Bastakoti

if (mysql_num_rows($Result) == 0) { 
    //error here
} else   {
    //loop here
}

Gives you a place for an error and a place to work with the results if they are notempty.

如果结果不为空,则为您提供出错位置和处理结果的位置。

回答by VolkerK

The easiest way is to use mysql_num_rows().

最简单的方法是使用mysql_num_rows()

$cnt = mysql_num_rows($Result);
if ( 0===$cnt ) {
    echo 'no records';
}
else {
    while( false!=($row=mysql_fetch_array($Result)) ) {
        // ...
    }
}

But keep in mind that this function returns the number of records that have been transferred from the MySQL server to the client so far. That's not necessarily the total record count. As long as you're using mysql_query()that's alright. But if you have e.g. large result sets you might want to use mysql_unbuffered_query(), and then you can't rely on mysql_num_rows() (for your purpose), because no records have been transferred before you call mysql_fetch_xyz().

但请记住,此函数返回到目前为止已从 MySQL 服务器传输到客户端的记录数。这不一定是总记录数。只要您使用mysql_query() 就可以了。但是如果你有大的结果集,你可能想要使用mysql_unbuffered_query(),然后你不能依赖 mysql_num_rows() (为了你的目的),因为在你调用 mysql_fetch_xyz() 之前没有记录被传输。

But instead you can use something like

但是你可以使用类似的东西

$cnt = 0;
while( false!=($row=mysql_fetch_array($Result)) ) {
    $cnt += 1;
    // ...
}
if ( 0===$cnt ) {
    echo 'no records';
}

Now you have an extra variable as a counter. You might want to avoid that and instead use something like

现在你有一个额外的变量作为计数器。你可能想避免这种情况,而是使用类似的东西

$row = mysql_fetch_array($Result);
if ( !$row ) {
    echo 'no records';
}
else {
    do {
        // ...
    } while( false!=($row=mysql_fetch_array($Result)) ) ;
}

With that approach you don't have an extra variable but some code duplication (the $x=fetch... line).
... make your choice ;-)

使用这种方法,您没有额外的变量,但有一些代码重复( $x=fetch... 行)。
... 做出你的选择 ;-)