如何检查 while($row = mysql_fetch_array in PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1937495/
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-25 04:21:42  来源:igfitidea点击:

How to check if results on while($row = mysql_fetch_array in PHP

phpmysql

提问by jrutter

Im trying to figure out how to handle this is no results are returned, how would I code that?

我试图弄清楚如何处理这个没有返回结果的问题,我将如何编码?

while($row = mysql_fetch_array($Result))

while($row = mysql_fetch_array($Result))

So like if there a results: print them out

所以就像如果有结果:打印出来

else: show a link

否则:显示链接

回答by mpen

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

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

if(mysql_num_rows($result) > 0) {
   while($row = mysql_fetch_array($result)) { ... }
} else {
  // show link
}

回答by Sampson

You can use mysql_num_rows()to tell you how many results are found. Using that with a simple if-statement, and you can determine what action to take.

您可以使用mysql_num_rows()来告诉您找到了多少结果。使用简单的if-statement,您可以确定要采取的操作。

if (mysql_num_rows($result) > 0) {
  // do while loop
} else {
  // show link
}

回答by Bill Karwin

Others suggest using mysql_num_rows()but you should be aware that that function works only if you use a buffered query. If you query using mysql_unbuffered_query(), the number of rows in the result is not available.

其他人建议使用,mysql_num_rows()但您应该知道该功能仅在您使用缓冲查询时才有效。如果使用 查询mysql_unbuffered_query(),则结果中的行数不可用。

I would use a simple flag variable:

我会使用一个简单的标志变量:

$found_row = false;

while ($row = mysql_fetch_array($result)) {
  $found_row = true;
  . . .
}

if ($found_row == false) {
  // show link
}

It may seem redundant to set $found_rowto true repeatedly, but assigning a literal value to a variable ought to be an insignificant expense in any language. Certainly it is small compared to fetching and processing an SQL query result.

重复设置$found_row为 true似乎是多余的,但为变量分配文字值在任何语言中都应该是微不足道的开销。当然,与获取和处理 SQL 查询结果相比,它很小。

回答by Binyamin

Use even shorter syntax without insignificant mysql_num_rowsto save processor time:

使用更短的语法mysql_num_rows来节省处理器时间:

if($result) {
   // return db results
} else {
   // no result
}

回答by VolkerK

This can be done without mysql_num_rows() or an additional (flag) variable

这可以在没有 mysql_num_rows() 或附加(标志)变量的情况下完成

if ( false===($row=mysql_fetch_array($result, MYSQL_ASSOC)) ) {
  echo 'no rows in result set';
}
else {
  do {
    echo $row['X'];
  } while ( false===($row=mysql_fetch_array($result, MYSQL_ASSOC)) );
}

but it duplicates the actual fetch command (one in the if-statement and one in the while-clause).

但它复制了实际的 fetch 命令(一个在 if 语句中,一个在 while 子句中)。

回答by jrutter

I might have figured it out:

我可能已经想通了:

if (!($row = mysql_fetch_array($descResult)))
     {
     echo "<tr><td>Add Link</td></tr>";
}