zazu Posted May 8, 2015 Share Posted May 8, 2015 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? Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted May 8, 2015 Share Posted May 8, 2015 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? Quote Link to comment Share on other sites More sharing options...
zazu Posted May 8, 2015 Author Share Posted May 8, 2015 <?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. Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted May 8, 2015 Solution Share Posted May 8, 2015 $result will only be false if there is an error. Not finding a record is not an error. Finding a record with a blank profile_image is not an error You need to check was a record found? if yes, what is the value in profile image? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.