Page 1 of 1

Images out of DB

Posted: Tue Mar 13, 2007 9:10 am
by Nightcat
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi guys

Got a problem and getting a bit desparate on my chances of solving it.

I am trying to display images that are stored in db

[syntax="sql"]
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)
);

As I work on it I managed to get one image to show, but I need to show all of them, currently there's 3 jpeg ons

Here's my code so far:[/syntax]

Code: Select all

<?php 
	$host = "";
	$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');

        $count = mysql_num_rows($result);


	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></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?
	for ($i=0; i<$count; $i++){
		echo $count;
		echo' <img src="' . $display . '" /><br />';
	}
		

?>

</body>
</html>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Mar 13, 2007 9:23 am
by Nightcat
To the admin

Thanks for the pointer :)

Posted: Tue Mar 13, 2007 9:25 am
by feyd
Images and HTML cannot be in the same stream as you are apparently attempting to do. However, since imagejpeg(), among others, return a boolean, you won't even get that far. :P In fact, the content is already image data, there's no need to involve GD at all.

What needs to happen is your script containing the HTML needs to make references to another script for all the image source links. That script must fetch the image requested. Each image will be a separate request to that script. This is why it's rarely thought to be a good idea to store images in a database.

Posted: Tue Mar 13, 2007 9:37 am
by Nightcat
Are there any samples on the net I could look at?

Cause I already searched, but might be puting in wrong search phrases :(

Posted: Tue Mar 13, 2007 9:39 am
by feyd
Try the phrase "image* database" in our search, minus the quotes. Make sure to check the "all terms" option.