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!

  • 1
    Assuming jpeg, change as needed. `echo '';` – AbraCadaver Feb 11 '19 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: https://stackoverflow.com/questions/7650587/using-javascript-to-display-a-blob. 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++;`) – tshimkus Feb 11 '19 at 20:56

1 Answers1

1

This should work

echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['image'] ).'"/>';
Navneet Krishna
  • 5,009
  • 5
  • 25
  • 44
Mr Tea
  • 141
  • 7