Trying to display image from mysql database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
lisasheppard
Forum Newbie
Posts: 1
Joined: Tue Dec 10, 2002 2:10 pm

Trying to display image from mysql database

Post 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
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post 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"
User avatar
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

Post 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
HormonX
Forum Commoner
Posts: 50
Joined: Tue Dec 10, 2002 7:43 pm
Location: Toronto

Re: Trying to display image from mysql database

Post 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
Post Reply