'Getting Image Path from SQLite Database

I have a path to an image stored in a SQLite table called images under column path. I am trying to take that path and use it as the src attribute for an HTML image. So far, it's not working. How do I convert the result of a query to a string the src can use?

<img src="<?php
  $database = new PDO("sqlite:database.sqlite");
  $database->query("SELECT path FROM images WHERE receiverId = '$_COOKIE['session']'");
?>" />


Solution 1:[1]

You can convert the image data into base64 and stick it in an tag. Currently, you are trying to write text outside of the image data, and the internet browser thinks your text is part of the image and thus throws an error.

Try something like this:

echo '<img src="data:image/jpeg;base64,' . base64_encode( $row['imageContent'] ) . '" />';
echo 'Hello world.';

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vineet1982