Displaying an image from a database...

Displaying an image from a database...

Posted by: Cobra
Posted on: 2007-11-07 21:37:00

I wrote a script that displays images that are stored in a database. It worked properly for one table of images, but now it doesn't. Instead of the page displaying the image, I get "Resource id #11." I searched the internet and found out that its not an error message. I didn't find out what it actually means, though.
This is the script:

if(isset($id))
{
$sql= mysql_query("SELECT * FROM `image` WHERE `image_id` = '$id'");
if(mysql_num_rows($sql) != 0)
{
$row = mysql_fetch_assoc($sql) or die(mysql_error());
$image_type = $row['image_type'];
$image = $row['image'];
header("Content-type: $image_type");
echo $image;
}
else echo "Invalid image ID";
}

The table contains image_id (varchar), image_type (varchar), and image (longblob). Any ideas?

Re: Displaying an image from a database...

Posted by: michael
Posted on: 2007-11-07 23:26:00

Why not just store the image on the file system and store the location of the image in your database? Storing an image in the db is wasteful of db resources.

Re: Displaying an image from a database...

Posted by: patricktan
Posted on: 2007-11-08 01:24:00

michael is right.

What you should do is

1. generate and unique name for the image (probably your image id)
2. store the image in a particular folder. It will be something like upload/image/00001.jpg
3. store the location of the image to database

When you want to display the image, retrieve the file path. It will be much better than storing image codes to database.

It does not only save the resource, but also improves efficiency. When you want to update any image, you don't really have to update the database but to simply replace the old image.

Re: Displaying an image from a database...

Posted by: Cobra
Posted on: 2007-11-08 05:48:00

I guess the consensus is that I should store the images on the server. I don't have any problems with that. I'm still curious as to what that resource message meant.

Re: Displaying an image from a database...

Posted by: Cobra
Posted on: 2007-11-08 06:08:00

Does is matter that I'm not uploading the file from a computer? I'm trying to save an edited version of
$image = imagecreatefromgif("an_image.gif");
A shove in the right direction would be appreciated.

Re: Displaying an image from a database...

Posted by: scjessey
Posted on: 2007-11-08 08:54:00

My guess is that you were trying to insert an image resource into a database, rather than an actual image. I would definitely go down the route suggested by the others. Apart from anything else, you will find your images are retrieved faster. They will also be cached.


-- si-blog --

Re: Displaying an image from a database...

Posted by: Cobra
Posted on: 2007-11-08 19:28:00

What is the difference between an image resource and a normal image?

Re: Displaying an image from a database...

Posted by: scjessey
Posted on: 2007-11-09 06:06:00

In reply to:

What is the difference between an image resource and a normal image?


An image resource is a bit like a connection - a bit like the difference between a connection to a MySQL database and the result of a query. It is more like a pointer to where a potential file exists, rather than an actual file. Please see the image functions for more information.


-- si-blog --

Tags: sqlvarcharmysql errormysql queryerror messagecontent typefetch