Reputation: 77
I have a mysql database with text and image (blob) information stored in it. I want to show all this information in a page using a PHP script, my problem is that not every inserted data contains image, so… when the PHP script is ran and there is an image stored on the database it shows correctly but when there is no image stored it displays a broken image symbol on the browser… How I can solve this so that the images are displayed when they exists on the database and avoid the broken image symbol when there is not?
My code is the following:
echo '<body bgcolor="#ffffff">';
echo "<p style='color:white;background-color:blue;font-family:arial black;font-size:20;text-align:center'>". $row['title'] . "</p>";
echo '<center><img src="data:image/jpeg;base64,'.base64_encode( $row['image'] ).'" width="300" eight="300"/></center>';
echo "<br>";
echo "<br><br>";
echo '<font size="3"><b><i>POR:' . $row['author'] . '</font></i></b><br>';
echo '<font size="3"><b><i>DATA: ' . $row['date'] . '</font></i></b><br><br>';
echo '<font size="3">' . $row['text'] . '</font><br>';
echo "<hr><hr>";
Upvotes: 0
Views: 152
Reputation: 956
All you have to do is add an if statement
, something like below would work.
if(isset($row['image']))
{
echo '<center><img src="data:image/jpeg;base64,'.base64_encode( $row['image'] ).'" width="300" eight="300"/></center>';
}
This will only display the echo if there is an image as specified by the $row['image']
variable
Upvotes: 0
Reputation: 26258
Try this:
if($row['image'] != '')
{
echo '<center><img src="data:image/jpeg;base64,'.base64_encode( $row['image'] ).'" width="300" eight="300"/></center>';
}
else
{
// show some text or a default "No image found"
}
Explanation: Show image only when some image is there in database.
Upvotes: 0
Reputation: 4544
Try checking if there is anything in the image row with a simple if.
echo '<body bgcolor="#ffffff">';
echo "<p style='color:white;background-color:blue;font-family:arial black;font-size:20;text-align:center'>". $row['title'] . "</p>";
if($row['image']) {
echo '<center><img src="data:image/jpeg;base64,'.base64_encode( $row['image'] ).'" width="300" eight="300"/></center>';
}
echo "<br>";
echo "<br><br>";
echo '<font size="3"><b><i>POR:' . $row['author'] . '</font></i></b><br>';
echo '<font size="3"><b><i>DATA: ' . $row['date'] . '</font></i></b><br><br>';
echo '<font size="3">' . $row['text'] . '</font><br>';
echo "<hr><hr>";
Upvotes: 1