I am trying to display an image from a MySQL database on a webpage using PHP.
I am aware that this is not the most efficient method, but still I would like to see how it is done....
To insert the image into the database I am using this 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();
To display the image on the website, I currently have this PHP code:
$result = mysqli_query($con,"SELECT * FROM table");
header('Content-Type: image/jpeg;');
echo "<table border='1' title='WEBCAM'>
<caption>WEBCAM</caption>
<tr>
<th>WEBCAM 1</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['webcamPic1'] .
"</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
On the webpage the file is displayed as a mess of symbols, like this:
ÿØÿàJFIFÿÛC $.' ",#(7),01444'9=82<.342ÿÛC
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€Ñ¨":µþ¦³Ó÷5¨Ò¼Ñ©ÿí÷]'âäŸöG'àèVqw«ý&Çì¹êøY&'¡Y²ñ¹Wg)±Âï ‡·òYCÿ_¥4÷NÜ.m˜,ƒ„¨g²3öEdÃBžrº¿^·º á•Flˆ!5Õ„NxZ0ˆP…¯>ꨢ ¡Aº£eFYD-!E•)‚ÓÙ›!•Xª2^Pct§•B‘ tŽmÐ) e¨$B‹Ç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“Ð÷Sòðùqþ‰ÇŸÇ“Ó>Âg=Ö^z>UëL”Àñu0ÇdgªŠåü‘Y8„%0@@dÃ(0ˆˆ¼ +| ל¦‚xEY„£÷TˆƒÙÁ[¿T-•” ìUYˆ?+ <#‰ESŽot‹€è(*Â4 E•jRBƒÂ ¸YEÌIÌ ¨°((×BèkïÑA@dvYX€”‰TIÀ©Pm·”ûP
Obviously I am doing something very wrong. What is the correct way to display the true image?
Thank you.