用字符串替换空行

时间:2012-12-27 20:33:45

标签: php mysql

我很难找到一个空的mysql查询的简单解决方案。我想要的是,对于一行被检索并且为空的行,默认情况下将替换为'no-image.png'。如果我的代码看起来像这样,我该如何处理呢?

$query="SELECT
venues.*
LEFT JOIN events 
ON events.VENUE_LOCATION = venues.ID  
where events.VENUE_LOCATION IS NULL
ORDER BY VENUE_NAME";

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($result)){
    echo "<td><IMG src=\"" . $row['IMAGE_URL'] . "\"></td>";
}

2 个答案:

答案 0 :(得分:2)

尝试

echo "<td><IMG src=\"" . ($row['IMAGE_URL']?$row['IMAGE_URL']:'no-image.png') . "\"></td>";

或者你可以使用if / else construct

if ($row['IMAGE_URL'] !== ''){
    $img = $row['IMAGE_URL'];
}
else{
    $img = 'no-image.png';
}
echo "<td><IMG src=\"" . $img . "\"></td>";

答案 1 :(得分:1)

您应该在输出变量之前检查变量,例如:

if(!empty($row['IMAGE_URL']))
  $img = $row['IMAGE_URL'];
else
 $img = 'no-image.png';

    echo "<td><IMG src=\"" .$img. "\"></td>";