在PHP数据库中,如果图像不可用,则隐藏损坏的图像
我有一个PHP查询,如果数据库中没有图像数据,我不想显示损坏的图像在PHP数据库中,如果图像不可用,则隐藏损坏的图像,php,mysql,Php,Mysql,我有一个PHP查询,如果数据库中没有图像数据,我不想显示损坏的图像 $sql = "SELECT * FROM Candidates c1 LEFT JOIN Parties p1 on p1.Id = c1.CurrentParty where c1.Id = 1 "; $result = $conn->query($sql); <div class="candidates"> <h5>Candidates for 2019 Vice-President El
$sql = "SELECT * FROM Candidates c1 LEFT JOIN Parties p1 on p1.Id =
c1.CurrentParty where c1.Id = 1 ";
$result = $conn->query($sql);
<div class="candidates">
<h5>Candidates for 2019 Vice-President Elections</h5>
<table class="table-bordered table">
<tr>
<th>Candidate Name</th>
<th>Party Name</th>
<th>Party Symbol</th>
<th>Candidate Image</th>
</tr>
<?php
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row["CandidateName"]; ?></td>
<td><?php echo $row["PartyName"]; ?></td>
<td><img src="<?php echo $row["PartySymbol"]; ?>" width="75" height="50" />
</td>
<td><img src="<?php echo $row["Photo"]; ?>" width="75" height="50" /></td>
</tr>
<?php }}?>
</table>
</div>
$sql=“从候选c1中选择*左侧加入p1上的参与方p1。Id=
c1.Id=1”的c1.CurrentParty;
$result=$conn->query($sql);
2019年副总统选举候选人
候选人姓名
党名
党徽
候选图像
“width=“75”height=“50”/>
“width=“75”height=“50”/>
请帮帮我!
谢谢使用下面的代码:
<td><?php echo ($row["PartySymbol"] != "")? '<img src='.$row["PartySymbol"].' width="75" height="50" />' : ''; ?> </td>
<td><?php echo ($row["Photo"] != "")? '<img src='.$row["Photo"].' width="75" height="50" />' : ''; ?></td>
您可以使用
onerror
属性
<img src="<?php echo $row["Photo"]; ?>" width="75" height="50" onerror="this.style.display='none';" />
“width=“75”height=“50”onerror=“this.style.display='none';" />
欢迎使用Stackoverflow,请尝试此代码。(假设我对您的问题的理解是正确的)
$sql=“从候选c1中选择*左侧加入p1上的参与方p1。Id=
c1.Id=1”的c1.CurrentParty;
$result=$conn->query($sql);
2019年副总统选举候选人
候选人姓名
党名
党徽
候选图像
“width=“75”height=“50”/>
“width=“75”height=“50”/>
我改进了两个方面,请检查它们:
而不是
,尽量使用
。这样会使代码更易于阅读onerror
属性。如果在src中找不到图像,这将在回退时执行JS$sql = "SELECT * FROM Candidates c1 LEFT JOIN Parties p1 on p1.Id =
c1.CurrentParty where c1.Id = 1 ";
$result = $conn->query($sql);
<div class="candidates">
<h5>Candidates for 2019 Vice-President Elections</h5>
<table class="table-bordered table">
<tr>
<th>Candidate Name</th>
<th>Party Name</th>
<th>Party Symbol</th>
<th>Candidate Image</th>
</tr>
<?php
if ($result->num_rows > 0) :
while ($row = $result->fetch_assoc()) :
?>
<tr>
<td><?= $row["CandidateName"]; ?></td>
<td><?= $row["PartyName"]; ?></td>
<td>
<img src="<?= $row["PartySymbol"]; ?>" width="75" height="50" />
</td>
<td>
<?php if($row["Photo"]): ?>
<img src="<?= $row["Photo"]; ?>" width="75" height="50" />
<?php endif; ?>
</td>
</tr>
<?php
endwhile;
endif;
?>
</table>
</div>