PHP If else statement that a database record is empty it will display a default picture


Hi basicly I am trying to create a simple IF statement that shows a default picture if one hasn't been entered in to my database. I am using server to store my picture and a database to store the file name, so I can get the image to display if it has a file name in the db but I want the If statement to say if the record is empty display this default image. I have some code here that I have tried however it doesn't work any thoughts? I tried a few other ways of doing it but they didn't work either.


Code so far:

//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM db*****") or die(mysql_error());

//Puts it into an array
while($info = mysql_fetch_array( $data ))

        <div class="member">
        <div class="imageSection">

        if($info['photo'] == '')
         {echo "<img class=\"memberImage\" src=images/default.jpg>";}
          else {echo "<img class=\"memberImage\" src=images/".$info['photo'] .">";}
        <div class="memberInfo">
<? Echo "<p><strong>Name: ".$info['nameMember'] . "</strong></p>";
Echo "<p>Position: ".$info['bandMember'] . " </p>";
Echo "<p>About Band Member ".$info['nameMember'] .":".$info['aboutMember'] . "</p>";
Echo "<p>Other Bands: ".$info['otherBands'] . " </p><br/></div></div><br class=\"clearBoth\"/>";



You could use file_exists, which will work even if the image is deleted manually.

$photo = 'images/'. $info['photo'];
if (file_exists($photo) == FALSE)
  $photo = 'images/default.jpg';
echo '<img class="memberImage" src="'. $photo .'"/>';
By : stukelly

empty() can catch the sort of conditions you are after, so try


empty() returns true if the parameter is '', NULL, false, '0', 0, or an empty array.

Are you sure the default value for the column is set to "". it could be set to null? although idk if that would cause it not work.

add the following code before the if statement

echo $info['photo'];

Another suggestion would be to trim the data before comparing it.

This video can help you solving your question :)
By: admin