0

Hi when im trying to display an image using php and mysql it only apears as the default no image found image is there anything im doing wrong here. i read a couple of different tutorials but they all seem to work where as my way does not

function DisplayImages($link){
    $qry = mysqli_query($link,"select * from images");
    while($row= mysqli_fetch_array($qry)){
    echo $row["name"];
    echo '<img src="<?php echo $row["image"]"/>';
    }
}

the images are uploaded as longblobs

6
  • Surely you need to at least convert it to a data url. Commented Sep 1, 2016 at 14:28
  • It could be a base64 image. Commented Sep 1, 2016 at 14:30
  • try something like that : ` $image_resource = imagecreatefromstring($row["name"]); header("Content-type: image/jpeg"); imagejpeg($image_resource);` Commented Sep 1, 2016 at 14:32
  • yes i belive i do i guess the tutorials i was looking at are wrong. any idea how to do that? Commented Sep 1, 2016 at 14:37
  • echo '<img src="data:image/jpeg;base64,' . base64_encode( $row['image'] ) . '" />'; i can use this but it will only display the jpegs Commented Sep 1, 2016 at 14:42

2 Answers 2

1

PHP is not recursively embeddable:

echo '<img src="<?php echo $row["image"]"/>';
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^

You're ALREADY in "PHP mode", so that <?php is not the start of a new PHP code tag. It's just the characters <, ?, etc... being stuffed into the string you're echoing.

And even if this COULD work, you have no ?> so it'd be a syntax error anyways.

Try:

echo '<img src="' . $row['image'] . '">';
or
echo "<img src=\"{$row['image']}\">";

If you'd done even basic debugging, like doing a "view source" in your browser to check the HTML you're building, you'd have seen that "php code" in your browser, meaning it never got executed.

Sign up to request clarification or add additional context in comments.

2 Comments

hmm just appears as random symbols ive been reading about converting it before displaying?
src is for a URL. you can't stuff the raw binary bytes of an image into it. If your DB data IS a blob of the image, you can't display it like that. Either convert to a data url, or do this in two stages <img src="showimage.php?id=XXX">, and then have that script pretty much literally just do echo $row['image'].
0
echo '<img src="data:image/jpeg;base64,' . base64_encode( $row['image'] ) . '" /height="50" width="50">';

thansk for the help it was weird that 3 tutorials i found all never mentioned of used the base54 thing but it works now thankyou

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.