1

如何判断一个php数组是否为空?我尝试了不同的方法,但它永远不会echo "no content";

while($row = mysql_fetch_array($result))
  {
        if(count($row['link']))
        {
                echo '<a id="link_' . $row['Id'] . '" href="' . $row['link'] . '" data="/short_info.php?id=' . $row['Id'] . '/">' . $row['title'] . '</a><div class="in...
        }
        else
        {
                echo "no content";
        }
  }
4

4 回答 4

9

It is never empty. But when data is exhausted, mysql_fetch_array returns false and your loop ends, so you're not going to see it in the loop, anyway.

于 2012-12-03T11:45:04.450 回答
5

$row is an array. $row['link'] is just a string. So you could you use:

if (strlen($row['link'])==0) {
  //do something
}

But if you want to check for no result (no data rows from mysql) then you could use:

if (mysql_num_rows($result)==0)
  echo "no content"
else {
  //your while loop
}
于 2012-12-03T11:45:01.797 回答
2

更新您的代码,如:

$num=mysql_num_rows($result);
    if ($num>0) {
        echo '$var is either 0, empty, or not set at all';
    }
    else{
        echo "no content";
    }
于 2012-12-03T11:47:23.380 回答
1

如果查询没有返回任何行,则此代码将永远不会进入 while 块,因为条件为 false .... 您可以编写:

$num=mysql_num_rows($result);

这将返回行数然后你写:

if($num==0) {echo "no content";}
else
{
while(
.........
}
于 2012-12-03T11:48:49.920 回答