title: Display an image from a MySQL database with PHP
body: <p>I am trying to display an image from a MySQL database on a webpage using PHP.<br> I am aware that this is not the most efficient method, but still I would like to see how it is done....</p>
  
  <p>To insert the image into the database I am using this code:</p>
  
  <pre><code>//Get File 
  File file = new File("C:\\Users\\blah\\blah\\blah\\WebcamImage\\" + name);
  FileInputStream fileStream = new FileInputStream(file);
  
  //Prepared Statement
  pStmt = connection.prepareStatement("INSERT INTO table (webcamPic1) values (?)");
  pStmt.setBinaryStream(1, fileStream, (int) file.length());
  pStmt.executeUpdate();  
  </code></pre>
  
  <p>To display the image on the website, I currently have this PHP code:</p>
  
  <pre><code>$result = mysqli_query($con,"SELECT * FROM table");
  
  header('Content-Type: image/jpeg;');
  
  echo "&lt;table border='1' title='WEBCAM'&gt;
      &lt;caption&gt;WEBCAM&lt;/caption&gt;
      &lt;tr&gt;
      &lt;th&gt;WEBCAM 1&lt;/th&gt;
      &lt;/tr&gt;";
  
  while($row = mysqli_fetch_array($result))
  {
      echo "&lt;tr&gt;";
      echo "&lt;td&gt;" . $row['webcamPic1'] . 
      "&lt;/td&gt;";
      echo "&lt;/tr&gt;";
  }
  echo "&lt;/table&gt;";
  
  mysqli_close($con);
  </code></pre>
  
  <p>On the webpage the file is displayed as a mess of symbols, like this:</p>
  
  <blockquote>
    <p>ÿØÿàJFIFÿÛC      $.' ",#(7),01444'9=82&lt;.342ÿÛC<br>
    2!!22222222222222222222222222222222222222222222222222ÿÀz£!ÿÄÿÄC!1AQa"q2‘¡±Á#Bá3RÑðbrñ$4CSc¢s’“ÂÒÿÄÿÄ(!1AQa"2q‘¡ðBÿÚ?ûö„ã=–Ù0Xa˜@•7BÀÙ”BTà÷@DÂ.€Å¬´
    7……Óc¨L1„¡£ì·2€Ä#?tû­7A‘öTl¢ƒÂeEUn@BØ@r°@Â%TCŠ(€±@,±@#„” RBÔB‘Â
    }’¢µ!•HHá›'±'')DÝ”%@Í7 Ì((hTm¥)U æð¦[ì¨‘6HE¾Pl%sA"æ[ÔûJ¨‹©OHh°7s
    Iáp€Ñ¨":µþ¦³Ó÷<em>5¨Ò¼Ñ©ÿí÷]'âäŸöG'àèVqw«ý&amp;Çì¹êøY&amp;'¡Y²ñ¹Wg)±Âï
    ‡·òYCÿ_¥4÷NÜ.m˜,ƒ„¨g²3öEdÃBžrº¿^·º á•Flˆ!5Õ„NxZ0ˆP…¯>ê¨¢
    ¡Aº£eFYD-!E•)Â‚ÓÙ›!•Xª2^Pct§•B‘</em> tŽmÐ) e¨$B‹Ç<em>Q¥•V¹Af:EøU”£
    Kð®¡OµÒk­ 8)œ•r›lØýÕ²Ð¢êRˆçÔm¡HÔp0,¹qàé[¢h~³kêäRË)vîîë·ãžØåßD«YpÕ«+ÑÆ9×
    gƒž0ºü;\ÚÕ›Pá$C</em>“Ð÷Sòðùqþ‰ÇŸÇ“Ó>Âg<code>=Ö^z&gt;UëL”Àñu0ÇdgªŠåü‘Y8„%0@@dÃ(0ˆˆ¼
    +| ×œ¦‚xEY„£÷TˆƒÙÁ[¿T-•” ìUYˆ?+ &lt;#‰ES</code>Žot‹­€è(*Â4 E•jRBƒÂ    ¸YEÌIÌ ¨°((×BèkïÑA@dvYX€”‰TIÀ©Pm·”ûP</p>
  </blockquote>
  
  <p>Obviously I am doing something very wrong. What is the correct way to display the true image?</p>
  
  <p>Thank you.</p>