Displaying an image from a database

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?

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.

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.

Save $97 (MAX Discount) with code: [color=#CC0000]97YES[/color] Sign Up NOW or More Codes Here

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.

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.

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.

Max discount on any plan with promocode SCJESSEYTOTAL

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.

Max discount on any plan with promocode SCJESSEYTOTAL