Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Displaying Images from MySQL

Status
Not open for further replies.

jw1234

Technical User
Oct 3, 2010
10
GB
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" "<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>
 
[ol][li]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. [/li]
[li]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. [/li]
[li]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. [/li]
[/ol]

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

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

Code:
//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['[red]id[/red]']);
//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 & Tech
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top