2

i have a small problem with my code. I'm trying to show an image out of my database on my homepage using php (pdo). Problem is: I don't really know how to let insert the variable of the image in the HTML img tag. Where is my mistake / How can I fix it?

I am saving the images in my mysql database as a blob (largeblob) and all images are .jpg and / or .png

<?php
  $db = new Dbh;
  $pdo = $db->connect();
  $counter = 0;
  $content = "";

  $statement = $pdo->prepare('SELECT * FROM images');
  $statement->execute();

  while ($row = $statement->fetch()) {          ?>

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

  <?php         }       
 ?>

The thing happening right now is, is that html just shows some kind of bit code full of strange symbols

Thanks for your help in advance!

2
  • 1
    Assuming jpeg, change as needed. echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image']).'"/>'; Commented Feb 11, 2019 at 20:49
  • If your images are being stored as blobs the src attribute isn't going to work as is. Here is a way to use javascript to render blob images: stackoverflow.com/questions/7650587/…. Note that using id as a selector as this example will not work in a loop structure, so you may want to use a class instead, and dynamically set a class identifier that increments each loop iteration (e.g., $classname = 'myimage-' . $i++;) Commented Feb 11, 2019 at 20:56

1 Answer 1

1

This should work

echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['image'] ).'"/>';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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