Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP数据库中,如果图像不可用,则隐藏损坏的图像_Php_Mysql - Fatal编程技术网

在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

我有一个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 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”/>
我改进了两个方面,请检查它们:

  • 请使用
    而不是
  • 另外,尽量避免使用
    ,尽量使用
    。这样会使代码更易于阅读
  • 此外,您还可以使用HTML
    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>