How to display the particular image in case if that image is not available in database? I have
PHP code is here:
<?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>
simply use @getimagesize
description w3school php.net this method will check if image actually exists . will return false if image deleted from db or from destination.
<?
$img="image url"; //orginal image url from db
if(!@getimagesize($img))
{
$img="default image" //if image not found this will display
}
?>
Update
for your code use like this
<?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>
Use a simple if condition
<img src="<? php if($row["file"]){ echo $row["file"] ; } else{// other image name}?>" height="100px" width="150px">
You can use ternary operator to do this, refer below and give a try
<?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>
If the file reference in the database is pointing to a file that may have been deleted, you have to check if the file still exists. Ideally, when he file is deleted, the reference in the database should be updated too, to avoid such problems, but that may not always be possible, especially when many people have access to FTP.
Check to see if file exists:
if(file_exists($yourImage)){
echo $yourImage;
}
else{
echo $defaultImage;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With