Jump to content

Verify if the photo exists in database else show a default photo


zazu
Go to solution Solved by Barand,

Recommended Posts

Hi guys,

 

I've inserted in my database and folder a specific photo for profile. Now i want to verify if the photo is added in database and if it is display it, and if is not display a default photo.

 

The code that i've used to show the photo is below, now i can't manage to do the secound part, wich is to display a default photo if there is none in database.

<?php
if ($result = $mysqli->query("SELECT profile_image FROM bonacuora_clients WHERE id='$id'")) {
while($row = mysqli_fetch_array($result))
  {
echo "<img src='photos/" . $row['profile_image'] . "' class='media-object img-circle' width='180' height='180'>";
  }
}
?>

Any suggestion?

Link to comment
Share on other sites

there are a number of options...

 

1 - set the default image to be the default value in the databse table, so that if nothing is entered in it will populate the path to your default image and no futher coding is required

2 - assuming the table holds a null record in the event of there not bing a user defined image then change the select statemt to use an ifnull conditional check to return the default path in the event that the resturned value from the database is null.

3 - perform conditional checkes in the php code that checks for the value returned from the database before using it to populate the img src and have it decide which path to load depending on the content of the result.

4 - check the filesystem to see if the path returned from the database exists and then load the defailt image path in the event that the result does not relate to an actual image file in the filesystem.

 

There are probably more, but that's all that jumps to mind at the moment.  Which woud you like to try?

Link to comment
Share on other sites

<?php
if ($result = $mysqli->query("SELECT profile_image FROM bonacuora_clients WHERE id='$id'")) {
while($row = mysqli_fetch_array($result))
  {
echo "<img src='photos/" . $row['profile_image'] . "' class='media-object img-circle' width='180' height='180'>";
  }
}
else {
echo "default photo is?";
}
?>

I;ve trying something like this, but it doesn't seems to work.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.