title: how to download blob based file from MySQL database in PHP?
body: <p>How can i make a PHP script that will allow me to download a file from a database in MySQL.
  I have the following table named <code>files</code> where the uploaded file is saved in a <code>BLOB</code> based field.    </p>
  
  <pre><code>+-------+----------+----------+------------+-------------------+--------+
  |fileID | fileName | fileType | fileSize   |fileData           | userID |
  +-------+----------+----------+------------+-------------------+--------+
  | 1     | file1    | JPEG     | 211258     |[BLOB - 206.3 KiB] | 1      |
  | 2     | file2    | PNG      | 211258     |[BLOB - 201.3 KiB] | 1      |
  +-------+----------+----------+------------+-------------------+--------+
  </code></pre>
  
  <p>I cannot figure out a way to call the file to download and every time i try, i get the binary data of the file, such as random numbers and symbols.</p>
  
  <p>I have tried this query where parameters are passed through (fileID, fileName, fileType) to a download.php page:</p>
  
  <pre><code>$id = mysqli_real_escape_string($link, $_GET['fileID']);
  $name = mysqli_real_escape_string($link, $_GET['fileName']);
  $type = mysqli_real_escape_string($link, $_GET['fileType']);
  
  $SELECT = "SELECT * FROM files WHERE fileID = $id AND fileName = $name ";
  $result = mysqli_query($SELECT, $link);
  $result = mysqli_fetch_assoc($result);
  
  header("Content-type: $type");  
  echo $result['fileData'];
  </code></pre>
  
  <p>But this leads to a blank page with no output.</p>
  
  <p>How can I download, for example, file1 as a JPEG file to my hard drive?</p>