5

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>
halfer
  • 19,824
  • 17
  • 99
  • 186
rajkumar
  • 365
  • 5
  • 10
  • 2
    We stopped using this API a long time ago. Join us. – Strawberry Apr 18 '15 at 09:54
  • 2
    Yeah save yourself lots of trouble and use PDO before you spend too many hours developing and have to change it all later. – Gary Hayes Apr 18 '15 at 09:57
  • check my answer it will work if image not exists in the destination – Manoj Dhiman Apr 18 '15 at 10:04
  • @ris can you completely insert your code in my file . I am not getting actually where i should put that piece of code. – rajkumar Apr 18 '15 at 10:21
  • I've removed the external link from this question - we try to discourage question posters from asking readers to fetch stuff. Please add whatever was in that link to your question - you can use the code formatting tool if it is console output. (External links like file lockers and paste boards are a bit more acceptable in comments, but it is still better if the question just contains everything). – halfer Apr 18 '15 at 10:37

6 Answers6

4

Use a simple if condition

<img src="<? php if($row["file"]){ echo $row["file"] ; } else{// other image name}?>" height="100px" width="150px">
Nishit Maheta
  • 6,021
  • 3
  • 17
  • 32
Sathya Baman
  • 3,424
  • 7
  • 44
  • 77
  • Thanks for the logic it works for only null image field .It do not work for the file that is deleted from database .Here is screenshoot https://docs.google.com/file/d/0B9wWzdgPkqb_eTlPOUpmX1k3Nk0/edit?usp=drivesdk – rajkumar Apr 18 '15 at 09:36
  • yes it wont work. though u delete the file, records are available in database. – Sathya Baman Apr 18 '15 at 09:42
4

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>
Manoj Dhiman
  • 5,096
  • 6
  • 29
  • 68
  • Thank you brother it work as expected :) Here is output https://docs.google.com/file/d/0B9wWzdgPkqb_ajh6blJ5X2RsSVU/edit?usp=drivesdk – rajkumar Apr 18 '15 at 10:31
2

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>
Anto S
  • 2,448
  • 6
  • 32
  • 50
  • Thanks for the code but it do not work at all ,Default image override all actual file also .Here is screenshoot https://docs.google.com/file/d/0B9wWzdgPkqb_U2NGZk9SNXN5a0E/edit?usp=drivesdk – rajkumar Apr 18 '15 at 09:57
  • It still do not work for file that is deleted from server .here is my screensoot https://docs.google.com/file/d/0B9wWzdgPkqb_cmtNRGRYRmZnYmM/edit?usp=drivesdk – rajkumar Apr 18 '15 at 10:08
  • If file deleted from server wont you update database image path? Anyway try my updated code – Anto S Apr 18 '15 at 10:10
  • No,bro my application won't update database once having particular image due to this reason i need default image – rajkumar Apr 18 '15 at 10:23
  • Anyway try my updated code, I modified it. But, it will be good approach to update database when you modify the file. – Anto S Apr 18 '15 at 10:26
1

You're storing the directory to the image within the varchar? if so try changing your database to store the image itself using BLOB as explained here

Then you can use an 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 a include, this may not be 100% relevant for your current project but can help down the line when needing to use more than one database connection.

  1. create a dbconnect.php file //name it what ever you want
  2. insert code into 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 you're trying to use a database use include_once 'dbconnect.php'
Community
  • 1
  • 1
Lero
  • 150
  • 1
  • 2
  • 10
1

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;
}
Gary Hayes
  • 1,728
  • 1
  • 15
  • 23
0
    <?php if($result["image"]==""){
         $path='upload/blankphoto.png';
         }
         else 
        {
            $path="upload/".$result['image'];
            }?>

    <img src="<?php echo $path?>" height="100" width="100">