Php 如果特定图像在数据库中不可用,如何显示默认图像?
如果数据库中没有特定图像,如何显示该图像?我有Php 如果特定图像在数据库中不可用,如何显示默认图像?,php,mysql,Php,Mysql,如果数据库中没有特定图像,如何显示该图像?我有 数据库名称:project 表名:图像 字段:id(int)、文件(varchar)[存储在此处的图像url]、名称(varchar)[图像描述] PHP代码如下: <?php $con=mysql_connect("localhost","root","") or die("no connection "); mysql_select_db("project")or die("no database exit"); echo "<h2
<?php
$con=mysql_connect("localhost","root","") or die("no connection ");
mysql_select_db("project")or die("no database exit");
echo "<h2 align='center'>Displaying image from database</h2>";
$res=mysql_query("SELECT * FROM image");
echo "<table>";
while ($row=mysql_fetch_array($res)) {
echo "<tr>";
echo "<td>";echo $row["id"];echo "</td>";
echo "<td>"; ?> <img src="<?php echo $row["file"]; ?>" height="100px" width="150px"> <?php echo "</td>";
echo "<td>"; echo $row["name"]; echo "</td>";
echo "</tr>";
}
?>
</table>
使用简单的if条件
<img src="<? php if($row["file"]){ echo $row["file"] ; } else{// other image name}?>" height="100px" width="150px">
“height=“100px”width=“150px”>
您可以使用三元运算符来执行此操作,请参阅下面的内容并进行尝试
<?php
$con=mysql_connect("localhost","root","") or die("no connection ");
mysql_select_db("project")or die("no database exit");
echo "<h2 align='center'>Displaying image from database</h2>";
$res=mysql_query("SELECT * FROM image");
echo "<table>";
while ($row=mysql_fetch_array($res)) {
// USE TERNARY OPERATOR HERE
$imagePath = (isset($row["file"]) && !empty($row["file"]) && file_exists($row["file"]))?$row["file"]:'img.default.jpg'; // REPLACE YOUR IMAGE PATHE HERE
echo "<tr>";
echo "<td>";echo $row["id"];echo "</td>";
echo "<td>"; ?> <img src="<?php echo $imagePath; ?>" height="100px" width="150px"> <?php echo "</td>";
echo "<td>"; echo $row["name"]; echo "</td>";
echo "</tr>";
}
?>
</table>
“height=“100px”width=“150px”>
您正在将目录存储到varchar中的映像中?如果是这样,请尝试更改数据库以使用BLOB存储图像本身,如前所述
然后可以使用if语句
<img src="<? php if($row["file"]){echo $row["file"];} else { echo '/directory/to/image.jpg'}?>" height="100px" width="150px">
“height=“100px”width=“150px”>
我还建议使用include,这可能与您当前的项目没有100%的相关性,但在需要使用多个数据库连接时,它可以帮助您完成这项工作
创建一个dbconnect.php文件//随意命名
在dbconnect.php文件中插入代码:
在试图使用数据库的任何文件中,请使用包含一次“dbconnect.php”
如果数据库中的文件引用指向可能已删除的文件,则必须检查该文件是否仍然存在。理想情况下,删除该文件时,数据库中的引用也应更新,以避免此类问题,但这可能并不总是可能的,尤其是当许多人可以访问FTP时
检查文件是否存在:
if(file_exists($yourImage)){
echo $yourImage;
}
else{
echo $defaultImage;
}
只需使用@getimagesize
说明此方法将检查图像是否确实存在。如果图像从数据库或目标中删除,则返回false
<?
$img="image url"; //orginal image url from db
if(!@getimagesize($img))
{
$img="default image" //if image not found this will display
}
?>
更新
对于您的代码,请这样使用
<?php
$con=mysql_connect("localhost","root","") or die("no connection ");
mysql_select_db("project")or die("no database exit");
echo "<h2 align='center'>Displaying image from database</h2>";
$res=mysql_query("SELECT * FROM image");
echo "<table>";
while ($row=mysql_fetch_array($res)) {
$img=$row["file"]; //orginal image url from db
if(!@getimagesize($img))
{
$img="default image" //if image not found this will display
}
echo "<tr>";
echo "<td>";echo $row["id"];echo "</td>";
echo "<td>"; ?> <img src="<?php echo $img; ?>" height="100px" width="150px"> <?php echo "</td>";
echo "<td>"; echo $row["name"]; echo "</td>";
echo "</tr>";
}
?>
</table>
感谢它只对空图像字段起作用的逻辑。它对从数据库中删除的文件不起作用。这是截屏是的,它不起作用。虽然你删除了文件,但数据库中有记录。我们很久以前就停止使用此API了。加入我们吧。是的,省去了很多麻烦,在花费太多时间之前使用PDOrs正在显影,以后必须全部更改。请检查我的答案。如果图像不存在于destination@ris你能在我的文件中完全插入你的代码吗?我不知道该把代码放在哪里。现在检查更新…谢谢你的代码,但它根本不起作用,默认图像也会覆盖所有实际文件。我在这里s截图对于从服务器上删除的文件仍然不起作用。这是我的屏幕。如果从服务器上删除的文件,你不会更新数据库映像路径吗?无论如何,请尝试我的更新代码否,兄弟我的应用程序在具有特定映像后不会更新数据库。由于这个原因,我需要默认映像无论如何,请尝试我的更新代码,我修改了它。但是,它当您修改文件时,这将是更新数据库的好方法。谢谢兄弟,它按预期工作:)这里是输出