-1

我很难找到空 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>";
}
4

2 回答 2

2

尝试

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

或者您可以使用 if/else 构造

if ($row['IMAGE_URL'] !== ''){
    $img = $row['IMAGE_URL'];
}
else{
    $img = 'no-image.png';
}
echo "<td><IMG src=\"" . $img . "\"></td>";
于 2012-12-27T20:35:50.747 回答
1

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

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

    echo "<td><IMG src=\"" .$img. "\"></td>";
于 2012-12-27T20:39:17.033 回答