How to display the default image if a specific image is not available in the database?

How to display a specific image if this image is not available in the database? I have

  • database name: project
  • table name: image
  • fields: id (int), file (varchar) [url saved here], name (varchar) [image description]

PHP code 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> 
+5
source share
5 answers

just use the @getimagesize description of w3school php.net this method checks if the image really exists. will return false if the image was deleted from db or from the destination.

 <? $img="image url"; //orginal image url from db if( !@getimagesize ($img)) { $img="default image" //if image not found this will display } ?> 

Refresh

for your use code 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> 
+4
source

Use the simple if condition

 <img src="<? php if($row["file"]){ echo $row["file"] ; } else{// other image name}?>" height="100px" width="150px"> 
+4
source

You can use the ternary operator for this, see below and 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> 
+2
source

Do you save the directory in the image in varchar? if so, try modifying your database to save the image using BLOB as described here

Then you can use the if statement.

 <img src="<? php if($row["file"]){echo $row["file"];} else { echo '/directory/to/image.jpg'}?>" height="100px" width="150px"> 

I would also recommend using include, it may not be 100% relevant for your current project, but it can help if you need to use more than one database connection.

  • create dbconnect.php file // name it whatever you want
  • paste the code into the dbconnect.php file:

<? php $con=mysql_connect("localhost","root","") or die ("no connection"); mysql_select_db("project")or die("no database exit");?>

  1. in any file that you use to use the include_once 'dbconnect.php' database include_once 'dbconnect.php'
+1
source

If the link to the file in the database points to a file that can be deleted, you should check if the file exists. Ideally, when a file is deleted, it is also necessary to update the link in the database to avoid such problems, but this may not always be possible, especially when many people have access to FTP.

Check if the file exists:

 if(file_exists($yourImage)){ echo $yourImage; } else{ echo $defaultImage; } 
+1
source

All Articles