Page 1 of 1

Problem Viewing Image in MySQL DB

Posted: Sun May 11, 2008 9:50 pm
by mirra1515
I am trying to simply take an image from a table and place it into a php file at a place of my choosing (whether it be next to text or in a table). I've gotten images to save to the table using a script I found and edited; however, when I enact the script to view the images it just views the images...nothing else. I need to be able to see more than just the image. In anycase....here's my current code:

Code: Select all

<?php
 
$con = mysql_connect("localhost","root","");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}
 
mysql_select_db("test_db", $con);
$result = mysql_query("SELECT * FROM ae_gallery WHERE id='$_GET[show]' LIMIT 1");
$row = mysql_fetch_assoc( $result );
$imageData = $row['data'];
$imageExt = $row['ext'];
 
header('Content-Length: '.strlen($imageData));
    header("Content-type: image/{$imageExt}");
 
echo $imageData;
 
mysql_close($con);
?>
It displays an image with an ID given by the URL via GET. Currently, it works, it just doesn't allow me to display anything other than the text. Sometimes it gives me an error, other times it just doesn't even display text I echo within the document. When I add html tags such as this:

Code: Select all

<html>
<body>
 
<?php
 
$con = mysql_connect("localhost","root","");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}
 
mysql_select_db("test_db", $con);
$result = mysql_query("SELECT * FROM ae_gallery WHERE id='$_GET[show]' LIMIT 1");
$row = mysql_fetch_assoc( $result );
$imageData = $row['data'];
$imageExt = $row['ext'];
 
header('Content-Length: '.strlen($imageData));
    header("Content-type: image/{$imageExt}");
 
echo $imageData;
 
mysql_close($con);
?>
 
THIS IS TEST TEXT
 
</body>
</html>
It gives me the following errors:

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\imagetest\showimage.php:4) in C:\xampp\htdocs\imagetest\showimage.php on line 18

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\imagetest\showimage.php:4) in C:\xampp\htdocs\imagetest\showimage.php on line 19

...And then spits out really nasty looking random characters that I suppose represent the binary making up the image in the table.

How could I fix this problem?

Re: Problem Viewing Image in MySQL DB

Posted: Mon May 12, 2008 2:54 am
by EverLearning
You're trying to output both HTML and image data. That's not going to work.
Call the file containing your first code snippet from a php file like this:

Code: Select all

<img src="image.php?show={$imageID}">