PHP MYSQL基本问题,如果查询空,隐藏图像

时间:2011-01-06 21:56:21

标签: php mysql sql

我问一个php mysql的基本问题。我进行查询并回显图像。在我的代码中,如果'.$row['image'].'为空,它将在浏览器中显示一个小的红叉图像。如果查询为空,如何隐藏图像?

$result = mysql_query("SELECT * FROM table WHERE catalog='image'"); 
while ($row = mysql_fetch_array($result))
{
echo '<img src="'.$row['image'].'" />';
}

3 个答案:

答案 0 :(得分:2)

为什么不检查$row['image']是否为空?

if(!empty($row['image'])){
  echo '<img src="'.$row['image'].'" />';
}

答案 1 :(得分:2)

   $result = mysql_query("SELECT * FROM table WHERE catalog='image'"); 
    while ($row = mysql_fetch_array($result))
    {
        if(trim($row['image']) != '')
        echo '<img src="'.$row['image'].'" />';
    }

答案 2 :(得分:2)

$result = mysql_query("SELECT * FROM table WHERE catalog='image'"); 
while ($row = mysql_fetch_array($result))
{
if(!empty($row['image'])){
echo '<img src="'.$row['image'].'" />';
}
}
相关问题