How do I retrieve Image in MYSQL using PHP?

I am currently creating an online shopping cart. In my database(test) I have a table images where all image are stored. Now my problem is that, when the moment I retrieve them, all the features in my database will displayed in my page (Something that I can't understand) not the real picture.
Could anyone can give me idea how to do it ? I Have my code below. Am I using the right Code ?? If it contains any Error, could you explain that to me? Thanks !

<?php
	$username = "myusername";
	$password = "mypassword";
	$host = "myhost";
	$database = "test";
	
	@mysql_connect($host,$username,$password) or die ("Cannot connect to database:" .mysql_error());
	@mysql_select_db($database) or die ("Unable to select database:" .mysql_error());
	
	$id = $_GET['id'];
	
	if(!isset($id) || empty($id))
	{
		die ("Please select your image");
	}
	else
	{
		$query = mysql_query("SELECT * FROM images WHERE id = '".$id."'");
		$row = mysql_fetch_array($query);
		$content =$row['data'];
		
		header('Content-type: image/jpg');
		echo $content;
	}
	
?>

let's say that the code you posted is for a file named getImage.php. Now on page1.php you need to show the image with id=23. All you have to do in page1.php is to put this:

<img src="http://yoursite.com/imageFetcher.php?id=23" />

Also, since you only need the `data` field, then use SELECT data FROM ...

Thank's !

It's working.. But what if I want to view image within the same page, how do I do this?"

use wrap your code in an if clause that checks to see if the id was provided. Then at the end just put the IMG tag with the desired id:

<?php
if( isset($_GET['id']) && !empty($_GET['id']))
{
 //rest of code goes here.
 ...
exit;
}
?> 
<img src="imageFetcher.php?id=12" />
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.