Images I pull from a database sometimes don't show in the browser, but they do if I view the source
I've started developing a site that pulls two images randomly from a database.
I assign a random number to a variable, between 1 and the amount of images there are stored in my database. I then query the database looking for that ID, and show the image in HTML image tags. This works fine and the two images appear randomly and I have checking to make sure they are never the same. But sometimes, one or both of the images won't appear on the page but if I view the source code, they are appearing there.
( I should note that the images aren't actually stored WITHIN the database, just the names of them are )
Here is the code that I am using to withdraw them from the database
function selectImage($id)
{
$query = "SELECT * FROM Images WHERE id = $id";
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($result) {
$image = $row['image'];
echo "<img src='" . $image . "' />";
}
}
Anyone know why 开发者_StackOverflowthis only displays the image sometimes? I'm refreshing to make it display another set of images.
Sometimes it can be tricky to sync up the filesystem with the database: If you are able to view the src in the HTML, and the image doesn't appear, then chances are the image doesn't exist, the name is stored incorrectly in your database, or perhaps the extension is being saved incorrectly (storing .jpg but the file is .jpeg or .JPG).
精彩评论