Page 1 of 1

Trying to display image from mysql database

Posted: Tue Dec 10, 2002 2:10 pm
by lisasheppard
Hello,

I'm trying to insert and display BLOB images from a mySQL database. I can't seem to get any of my code to work. It just displays a cross where the image should be. I am fairly new to php so it is probably something really obvious that I'm missing.

My code to display the image is as follows - (binProductImage is a medium blob in the database):

include_once ('../includes/openconnection.php');

$get_image = "select binProductImage, filetype from tblProducts where pk_iProductID = 2";

$get_image_result = mysql_query($get_image) or die("Couldn't get image.");

$product_image = mysql_result ($get_image_result,0,"binProductImage");
$filetype = mysql_result ($get_image_result,0,"filetype");

header("Content-type: $filetype");
echo $product_image;

I'm calling this from another page by writing:

<IMG SRC="show_image.php">

I would really appreciate if anybody can help me.
Cheers,

Lisa

Posted: Tue Dec 10, 2002 2:36 pm
by JPlush76
lisa, is there a reason you're storing images as BLOBS?

a much easier and cleaner solution is to just store the path to the image

so your field would hold: "images/mypic.jpg"

Re: Trying to display image from mysql database

Posted: Tue Dec 10, 2002 6:26 pm
by puckeye
First have you tried without the header to see what error(if any) was returned by PHP?

Second if the image stored in the database was originally an image that didn't receive any transformation as it was inserted into the db then you simply need to output the field, header wouldn't be required.

For example I use this peace of code to display images that are located outside the web server path:

Code: Select all

<?php
readfile($image);
?>
Calling this script like

Code: Select all

&lt;IMG SRC="image.php?image=aaa.gif"&gt;
works well.

Where $image is the location of the file. The header function is not needed at all since the file is an image which already have it's own headers.

The same thing applies to your image stored in a database instead then directly on a drive...

Puckeye

Re: Trying to display image from mysql database

Posted: Tue Dec 10, 2002 8:11 pm
by HormonX
lisasheppard wrote: $get_image = "select binProductImage, filetype from tblProducts where pk_iProductID = 2";
The onely thing i would change is this ...

$get_image = "select binProductImage, filetype from tblProducts where pk_iProductID='2'";

Best Regards,

HormonX