Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display default image if particular image is not available in database?

Tags:

php

mysql

How to display the particular image in case if that image is not available in database? I have

  1. database name:project
  2. table name: image
  3. fields: id (int), file (varchar) [image url stored here], name (varchar) [image description]

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>
like image 648
rajkumar Avatar asked Apr 18 '15 09:04

rajkumar


4 Answers

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>
like image 128
Manoj Dhiman Avatar answered Nov 14 '22 23:11

Manoj Dhiman


Use a simple if condition

<img src="<? php if($row["file"]){ echo $row["file"] ; } else{// other image name}?>" height="100px" width="150px">
like image 35
Sathya Baman Avatar answered Nov 14 '22 22:11

Sathya Baman


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>
like image 45
Butterfly Avatar answered Nov 14 '22 22:11

Butterfly


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;
}
like image 44
Gary Hayes Avatar answered Nov 14 '22 21:11

Gary Hayes