Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...This has been the MOST helpful forum that I have been a part of and I want to say thank you. The tips, tricks and helpful advice that you all contribute to have been lifesavers in many instances..."

Geography

Where in the world do Tek-Tips members come from?

PHP Displaying Images from MySQL

jw1234 (TechnicalUser)
15 Jul 12 15:19
Right I have a problem and the standard way of resolving it does not appear to work in this instance so I need a way to solve it.

I have a MySQL database with table called properties in - This works fine now each row in my database has an image attched which is the picture of a property

I can return all the text and numeric data fine - no problem there at all
The picture is correctly store as a BLOB in the database

As has been suggested i'e changed the content type to be image/jpeg
this is currently commented out as it does not work

I've attached my files here

THIS IS THE RESULTS FILE....

<!DOCTYPE HTML PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<style type="text/css">

<!--
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
background-color: #FFFFFF;
}
a:link {
color: #000000;
text-decoration: none;
}
a:visited {
text-decoration: none;
}
a:hover {
text-decoration: none;
}
a:active {
text-decoration: none;
}
.style9 {font-size: 14px}
.style21 {
font-family: Arial, Helvetica, sans-serif;
font-weight: bold;
}
.style22 {
font-family: Arial, Helvetica, sans-serif;
color: #000000;
font-weight: bold;
font-size: larger;
}
.style24 {
font-size: x-small;
font-family: Tahoma;
}
.style25 {color: #000000; font-family: Arial, Helvetica, sans-serif;}
-->
</style>
</head>

<body>
<table width="285" height="341" border="0" align="center" cellspacing="2">
<tr>
<td height="22" align="center" valign="top" bgcolor="#FFCC00" class="style22"><span class="style21">Agents Name &amp; Logo Here</span></strong>
</div>
</tr>

<tr>
<td height="38" bgcolor="#FFCC00" class="style9"><div align="center" class="style25">Mobile Property Search Results Page</div>
</tr>
<tr>
<td height="246" bgcolor="#0000FF" class="style9">
<?php
// Turn Off All Error Reporting
error_reporting(1);

//Connect To Database
$conn = mysql_connect("localhost","web233-smt-web_1","john041066") OR die ("Connection Error: ".mysql_error());

if(!$conn)
{
echo mysql_error();
}

$db = mysql_select_db("web233-smt-web_1",$conn) OR die ("DB Error: ".mysql_error());

if(!$db)
{
echo mysql_error();
}

//Get Values Sent Over Search Form
$saleorrent = $_GET['prop_sr'];
$propertytype = $_GET['prop_type'];
$bedrooms = $_GET['prop_bed'];
$minimum = $_GET['min_price'];
$maximum = $_GET['max_price'];


//Build The Query - This Works Just fine providing you are using the MySQL fetch array method !!!
$query = "SELECT * FROM properties
WHERE prop_sr = '$saleorrent'
AND prop_type = '$propertytype'
AND prop_bed = '$bedrooms'
AND prop_price >= '$minimum'
AND prop_price <= '$maximum'";

$result = mysql_query($query) or die(mysql_error());

//header("Content-type: image/jpeg");
while($row = mysql_fetch_array($result)){

//echo $row['prop_photo']; echo $row['prop_sr'];
echo $row['prop_type'];
echo $row['prop_bed'];
echo $row['prop_addr'];
echo $row['prop_price'];
}
?>

</tr>
<tr>
<td height="21" align="center" valign="top" bgcolor="#FFCC33"><span class="style24">By Computer Solutions © 2012</span><span class="style24"></span><span class="style9"><br />
</span></td>
</tr>
<td height="2">
</table>

</body>
</html>
vacunita (Programmer)
15 Jul 12 20:31
  1. To show an image in a website you need an image tag. echoing out the binary data of an image will only result in garble being echoed into the web page.
  2. Since the image tag requires a file from which to load the image, you'll need to build a separate script to fetch the image. The script needs to have nothing output to screen but the image information, and the correct headers.
  3. You can then call this script in an image tag to load the appropriate image. Assuming each row has a Id you can use to identify it, you can use that to get the correct image.

CODE --> ImageTagInOriginalCode"

while($row = mysql_fetch_array($result)){

echo "<img src='loadPhoto.php?id='" . $row['prop_id'] . "alt='Picture' />";
echo $row['prop_type']; 

CODE --> ImageFetchingScript

//Connect To Database
$conn = mysql_connect("localhost","web233-smt-web_1","john041066") OR die ("Connection Error: ".mysql_error());
if(!$conn)
{
echo mysql_error();
}
$db = mysql_select_db("web233-smt-web_1",$conn) OR die ("DB Error: ".mysql_error());
if(!$db)
{
echo mysql_error();
}

$id = mysql_real_escape_string($_GET['id']);
//Build The Query - This Works Just fine providing you are using the MySQL fetch array method !!!
$query = "SELECT prop_photo FROM properties WHERE prop_id = $id LIMIT 1";
$result = mysql_query($query) or die(mysql_error());	
header("Content-type: image/jpeg");	
$row = mysql_fetch_array($result);

echo $row['prop_photo']; 

There's several other reasons why storing images in a database is not suggested. But the need for the extra script is a major point against it.

Its suggested to store it in a folder in the server file system instead, and have in the DB simply a path pointing to the image.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web &amp; Tech

jw1234 (TechnicalUser)
15 Jul 12 21:07
Thanks Phil thats good of you i've actually done it now using base64 encoding where I do not need any of that code at all. i'm against using a filesystem as it is not terribly secure really, although i'll keep your code sample for another upcoming prject where it could come in useful. I figured that if I sat long enough at it i'd eventually crack it and I did.

I do a Base64 encode on the fly of the image and spit the image data back into an IMG SRC tag, works in everything except IE. but then IE is pretty well dead anyway. This is a mobile application where IE is not going to be used.


Heres part of my code

while($row = mysql_fetch_array($result)){

echo '<center><img src="data:image/jpeg;charset=utf-8;base64,' . base64_encode( $row['prop_photo'] ) . '" /></center>';

echo ('Sale/To Let: ');
echo $row['prop_sr']."<br>";

echo ('Property: ');
echo $row['prop_type']."<br>";

echo ('Bedrooms: ');
echo $row['prop_bed']."<br>";

echo ('Address: ');
echo $row['prop_addr']."<br>";

echo ('Price: ');
echo $row['prop_price']."<br>";
}


So much better than having an upload folder full of images which could easily be hacked into.

Ironically the part that I want to finish it with is that i'd like white echo text when used with ARRAY DATA like this that is proving hard to find though


Feel ftree to use my code your self - you are very welcome and thanks for the help

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close