检查获取的数组是否为空 PHP?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11790617/
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
Check if the fetched array is empty or not PHP?
提问by monk
I am trying to check if the mysql_fetch_array() function returns an empty array or not. But my code doesn't seem to work. Here I want to ensure that if the array is empty I want to display under construction message.
我正在尝试检查 mysql_fetch_array() 函数是否返回空数组。但是我的代码似乎不起作用。在这里,我想确保如果数组为空,我想显示正在建设中的消息。
Code :
代码 :
$queryContents= queryMembers();
$exeQuery = mysql_query($queryContents);
while($fetchSet = mysql_fetch_array($exeQuery)) {
if(count($fetchSet) == 0) {
echo "This Page is Under Construction";
}else{
// something else to display the content
}
}
How do I check to acheive such feature ?
我如何检查以实现这样的功能?
回答by pahan
use mysql_num_rows to count number of rows. try this.
使用 mysql_num_rows 来计算行数。尝试这个。
$exeQuery = mysql_query($queryContents);
if(mysql_num_rows($exeQuery)== 0){
echo "This Page is Under Construction";
}
else{
while($fetchSet = mysql_fetch_array($exeQuery)) {
// something else to display the content
}
}
回答by jeremy
You really should be using mysql_num_rowshttp://us2.php.net/manual/en/function.mysql-num-rows.php
你真的应该使用mysql_num_rowshttp://us2.php.net/manual/en/function.mysql-num-rows.php
However, on a side note, you should use php empty()instead. http://us2.php.net/empty
但是,附带说明一下,您应该改用 php empty()。http://us2.php.net/empty
回答by SHAKIR SHABBIR
When you use mysql_fetch_array(), it returns the rows from the data set one by one as you use the while loop.
当您使用 mysql_fetch_array() 时,它会在您使用 while 循环时一一返回数据集中的行。
If there will be no record, while loop wont execute. In this case, declare a boolean variable and make it true if it enters the while loop. Like:
如果没有记录,while 循环不会执行。在这种情况下,声明一个布尔变量并在它进入 while 循环时使其为真。喜欢:
$queryContents= queryMembers(); $exeQuery = mysql_query($queryContents); $recordExists = 0; while($fetchSet = mysql_fetch_array($exeQuery)) { if($recordExists == 0 ) $recordExists = 1; // something else to display the content } if($recordExists == 0 ){ echo "This Page is Under Construction"; }
$queryContents= queryMembers(); $exeQuery = mysql_query($queryContents); $recordExists = 0; while($fetchSet = mysql_fetch_array($exeQuery)) { if($recordExists == 0 ) $recordExists = 1; // something else to display the content } if($recordExists == 0 ){ echo "This Page is Under Construction"; }
Hope this works!
希望这有效!
回答by Prasanth
回答by Bhuvan Rikka
Try this
尝试这个
if(empty($fetchSet)
{
echo "This Page is Under Construction";
}
else
{
// something else to display the content
}
回答by AndrewR
Your code inside the while loop never runs if there are no results. mysql_fetch_array returns null/false if there are no more results. What you need yo do is check with mysql_num_rows first, before the while.
如果没有结果,while 循环中的代码永远不会运行。如果没有更多结果,mysql_fetch_array 返回 null/false。您需要做的是先检查 mysql_num_rows,然后再检查。
$queryContents= queryMembers();
$exeQuery = mysql_query($queryContents);
if(mysql_num_rows ($exeQuery) == 0) {
echo "This Page is Under Construction";
}
while($fetchSet = mysql_fetch_array($exeQuery)) {
// something else to display the content
}

