Q695 Posted July 9, 2009 Share Posted July 9, 2009 How do I create an image from a database if there's one avalible, or if there isn't one I use a default photo? Here is what I have so far: <?php include "../link/log.php"; include "../link/dead.php"; $sql="SELECT * FROM players WHERE id='$id';"; $result=@mysql_query($sql, $con) or die(death($sql)); $u_row=mysql_fetch_array($result); if ($u_row[image]){ echo $u_row[image]; } else { ?> <img src="sword.jpg"> <?php } ?> note:The two other files are in a different folder at the same level. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 9, 2009 Share Posted July 9, 2009 Your code has the right logic, but there are just a few problems. 1. Unless you are needing the other fields, just query for the image. 2. You need to enclose the array index name within quotes. 3. You are only checking if the query returns a value for "image". You need to check if the value contains anything. I think that check might work if you are setting the value as NULL, but it won't work if the value is an empty string - which is what most people do 4. Unlsee you are storing the image as the complete html source to display the image (not recommended) you need to add the HTML tags to the path. here is how I would do it: <?php include "../link/log.php"; include "../link/dead.php"; $sql="SELECT img FROM players WHERE id='$id';"; $result=@mysql_query($sql, $con) or die(death($sql)); $u_row=mysql_fetch_array($result); //Determine the image to display if ($u_row['image'] != '') { $img = $u_row[image]; //Add the appropriate path } else { $img = "sword.jpg"; //Add the appropriate path } //Display the image echo "<img src=\"{$img}\">"; Quote Link to comment Share on other sites More sharing options...
Q695 Posted July 10, 2009 Author Share Posted July 10, 2009 The image is physically being storred in the database as a blob, since it's in it's own folder with a pointer in the image file, kind of like how captchas are done. 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.