title: Displaying a Base64 images from a database via PHP
body: <p>I have this database that contains images as strings. Those strings look something like this:</p>
  
  <pre><code>data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD...
  </code></pre>
  
  <p>I need to create a link that will display this image. Like <code>something.com/?id=27</code> is an image. All the images are in jpeg format. Here's what I've tried but didn't work:</p>
  
  <pre><code>&lt;?php
    $host = "smth";
    $user = "smth";
    $pass = "smth";
    $db_name = "smth";
    $dbh = new PDO("mysql:host=$host;dbname=$db_name", $user, $pass);
    $dbh-&gt;exec("SET NAMES utf8");
    $q = $dbh-&gt;prepare("select content from img where id = :id");
    $q-&gt;execute(array(':id'=&gt;$_GET['id']));
    $row = $q-&gt;fetch(PDO::FETCH_BOTH);
    header("Content-type: image/jpeg");
    echo $row['content'];
  ?&gt;
  </code></pre>
  
  <p>The data is being fetched correctly but the image is not displayed.</p>
  
  <p>I need to be able to use this link like this <code>&lt;img src="mysite.com?id=21" /&gt;</code> and I do NOT want this solution: <code>&lt;img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABA..." /&gt;</code></p>
  
  <p>Thanks!</p>