PHP Display image out of DB
hi guys
I've hit another problem :( Got a db which holds the images I need for the site. There is a number of reasons for doing that and other options were checked out.
I also got a script which supposed to show those images, unfortunately it only shows the first image in db unless specificaly told display another image, but I need it to desplay all the images stored or multiple entries.
Here's the table settings:
CREATE TABLE upload (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30) NOT NULL,
type VARCHAR(30) NOT NULL,
size INT NOT NULL,
content MEDIUMBLOB NOT NULL,
PRIMARY KEY(id)
);
and here is the last variation of the code
<?php
$host = "localhost";
$user = "";
$password = "";
$database = "";
$connection = mysql_connect($host,$user,$password)
or die ("Couldn't connect to a server");
$database = mysql_select_db($database, $connection);
$query = "SELECT id, type, content FROM upload";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty <br>";
}
else
{
while($results = mysql_fetch_array($result))
{
$content = $results["content"];
$type = $results["type"];
$id = $results["id"];
$image= imagecreatefromstring($content);
switch ($type){
case "image/jpeg":
header("Content-type: image/jpeg");
$display = imagejpeg ($image);
imagedestroy($image);
break;
case "image/png":
header("Content-type: image/png");
$display = imagepng($image);
imagedestroy($image);
break;
case "image/gif":
header ("Content-type: image/gif");
$display = imagegif($image);
imagedestroy($image);
break;
}
}
}
?>
<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?
for ($i=0; i<count($display); $i++){
echo $id;
echo' <img src="' . $display . '" /><br />';
}
?>
</body>
</html>
So far there are definately 3 images in db, they are all jpegs. Any help welcome :)