Page 1 of 1

MySQL PHP and BlOB Inages

Posted: Wed Dec 21, 2005 2:34 pm
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

Posted: Wed Dec 21, 2005 3:35 pm
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.

Posted: Wed Dec 21, 2005 4:10 pm
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

Posted: Wed Dec 21, 2005 4:28 pm
by John Cartwright
try

Code: Select all

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