如果存储在数据库中的图像文件路径不存在,如何显示默认图像?

时间:2018-03-13 17:50:45

标签: php html

如果我存储在数据库中的图像文件路径不存在,我想询问如何显示默认图像。我已经浏览了链接,看看它是否可以帮助我查询但是它没有用。

至少我能做到的是:

<?php
        $con = new mysqli("localhost" ,"root" ,"" ,"user_databases");

        $iquery = mysqli_query($con, "SELECT map_image_filepath FROM buildings");

        while($row = mysqli_fetch_array($iquery))
        {
          echo "<img src='".$row['map_image_filepath']."'>";
        }

        mysqli_close($con);
?>

where&#34; map_image_filepath&#34;是我的建筑物表格中存储文件路径的列名称。

我真的不知道下一步该做什么。我希望你们能帮忙:(

1 个答案:

答案 0 :(得分:0)

看看我的例子:

<?php
        $con = new mysqli("localhost" ,"root" ,"" ,"user_databases");

        $iquery = mysqli_query($con, "SELECT map_image_filepath FROM buildings");

        while($row = mysqli_fetch_array($iquery))
        {

    $filename = $row['map_image_filepath'].".png";//here you can put jpg also
    if(file_exists($filename)) {
        echo "<img src="{$filename}">";
    }else{
        echo "<img src="NoImage.jpg">";
    }

}

        mysqli_close($con);
?>
相关问题