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
Trying to display image from mysql database
Moderator: General Moderators
-
lisasheppard
- Forum Newbie
- Posts: 1
- Joined: Tue Dec 10, 2002 2:10 pm
- puckeye
- Forum Contributor
- Posts: 105
- Joined: Fri Dec 06, 2002 7:26 pm
- Location: Joliette, QC, CA
- Contact:
Re: Trying to display image from mysql database
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:
Calling this script like 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
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);
?>Code: Select all
<IMG SRC="image.php?image=aaa.gif">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
The onely thing i would change is this ...lisasheppard wrote: $get_image = "select binProductImage, filetype from tblProducts where pk_iProductID = 2";
$get_image = "select binProductImage, filetype from tblProducts where pk_iProductID='2'";
Best Regards,
HormonX