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 !

	$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");
		$query = mysql_query("SELECT * FROM images WHERE id = '".$id."'");
		$row = mysql_fetch_array($query);
		$content =$row['data'];
		header('Content-type: image/jpg');
		echo $content;

Edited by __avd: Added [code] tags.

6 Years
Discussion Span
Last Post by hielo

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?"

Edited by amulet_john: n/a


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:

if( isset($_GET['id']) && !empty($_GET['id']))
 //rest of code goes here.
<img src="imageFetcher.php?id=12" />
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.