MySQL PHP and BlOB Inages

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
chrisostomos
Forum Newbie
Posts: 2
Joined: Wed Dec 21, 2005 2:25 pm

MySQL PHP and BlOB Inages

Post by chrisostomos »

Hi,

I am have made a scipt that uploads images to a MySQL database with blob data type from PHP.
Now i am just wondering how i can retrieve the images and display them. I have managed to display One image, but when i try to display multiple images it just doesnt seem to work, and that where i have been stuck for a while now.

Here is the code...

Code: Select all

<?php
	$modelName = "kathy";

	$hostname = "localhost";
	$username = "root";
	$password = "";
	$databaseName = "newwavemodels";

	// Connect to database
	$errmsg = "";
	if (! mysql_connect($hostname,$username,$password)) 
	{
        $errmsg = "Cannot connect to database";
	}

	mysql_select_db($databaseName);
	
	$result = mysql_query("select imgdata from pix");

	$index = 0; 
	while($row = mysql_fetch_array($result))
	{
        header("Content-type: image/jpeg");
		  echo mysql_result($result, $index);
		  $index++;
	} 
?>
My table structure is the following:
CREATE TABLE `pix` (
`pid` varchar(11) default NULL,
`title` varchar(10) NOT NULL default '',
`imgdata` longblob,
`thumbImage` longblob,
PRIMARY KEY (`title`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Thanks for any input

Chris Vicatou
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

What you need to do is make a PHP file that can masquerade as an image. When you want a particular image to appear, call that PHP file, sending along the id of the image in the filename. For example:

Code: Select all

<img src = "getDBImage.php?id=12">
In that getDBImage.php file, access the database, get the appropriate image data, send out your JPG/GIF/PNG/Whatever headers, then dump the image data. To the browser it'll appear as if it's just displaying an ordinary image file.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
chrisostomos
Forum Newbie
Posts: 2
Joined: Wed Dec 21, 2005 2:25 pm

Post by chrisostomos »

Hey again,

I am attempting to do what you are telling me and still doesnt seem to work

I edited the code with the following

Code: Select all

header("Content-type: image/jpeg");
	echo "<img src = 'testing3.php?title=2'>";
Thanks for any input
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

try

Code: Select all

$file = "filename.jpg";
header("Content-type: image/jpeg");
readfile($file);
Post Reply