Laycoonz
Laycoonz

Reputation: 195

Display if exist image from mysql with PHP

I setup a comment form with the option to submit an image, I want now to display the image only if exist.

This is the php script that I have, I know it should be related to an "if" statement but I don't know how to do it

Any help would be appreciated

<?php
$dbLink = mysql_connect("xxx","xxx","xxx");
mysql_query("SET character_set_results=utf8", $dbLink);
mb_language('uni');
mb_internal_encoding('UTF-8');

$getquery=mysql_query("SELECT * FROM comment ORDER BY id DESC");

while($rows=mysql_fetch_assoc($getquery)) {
    $id=$rows['id'];
    $name=$rows['name'];
    $comment=$rows['comment'];
    echo '<b>' . $name . '</b>' . '<br/>' . '<br/>' . $comment . '<br/>' . '<br/>' . '<img src="data:image/jpeg;base64,'.base64_encode($rows['imageData'] ).'"/>' . '<br/>' . '<br/>' . '<hr size="1"/>';
}
?>

Upvotes: 0

Views: 63

Answers (1)

reza
reza

Reputation: 1507

Just divide the echo into 3 parts

echo '<b>' . $name . '</b>' . '<br/>' . '<br/>' . $comment . '<br/>' . '<br/>' ;

if(isset($rows['imageData']) && $rows['imageData'] != '') 
   echo '<img src="data:image/jpeg;base64,'.base64_encode($rows['imageData'] ).'"/>'.'<br/>' . '<br/>' ;

echo   '<hr size="1"/>';

Upvotes: 2

Related Questions