Problem Viewing Image in MySQL DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mirra1515
Forum Commoner
Posts: 29
Joined: Fri Apr 25, 2008 3:17 am

Problem Viewing Image in MySQL DB

Post 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?
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: Problem Viewing Image in MySQL DB

Post 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}">
Post Reply