Getting an image from a MySQL database using PHP

Jens

Well-Known Member
Joined
Jul 12, 2009
Messages
1,867
Location
Belgium
Car(s)
BMW 116d, W124 250D
Hi coders of Final Gear!
I'm working on a blog and I'm trying to display a .JPEG image from my database.

I have a table called tblnews with id, date, title, content, author and the image as a longblob.

Here's the code:

Code:
<?php
include( 'sql_connect.php' );

$image = stripslashes($_REQUEST[imname]);
$rs = mysql_query("select * from tblnews where filename=\"".
addslashes($image).".jpg\"");
$row = mysql_fetch_assoc($rs);
$imagebytes = $row[image];
header("Content-type: image/jpeg");
print $imagebytes;
?>
and the HTML is

Code:
<img src= picscript.php?imname=test>
To get the data such as title and content I use another script that works, but the image is doesn't show. Do I have to combine the two scripts or perhaps a new table just for images...
Or is my script just plain wrong?

Thanks in advance.
 
Last edited:

narf

Sgt. Maj. Buzzkill
DONOR
Joined
Feb 1, 2008
Messages
18,310
Location
Kiel/Wherever, Germany
Car(s)
'19 BMW M240i
You could try to narrow down the issue further than "the image doesn't show". For example, you could check if the retrieval of the data worked by looking at the amount of data stored in $imagebytes. That way you can work your way towards the problem.

Oh yeah, attribute values in HTML should be quoted, as in '<img src="foo" />'. That most likely is not your cause though :tease:
 

Jens

Well-Known Member
Joined
Jul 12, 2009
Messages
1,867
Location
Belgium
Car(s)
BMW 116d, W124 250D
To be more specific, the image doesn't get selected in the database.
The connection works, so the problem has to be $image or $rs
 

narf

Sgt. Maj. Buzzkill
DONOR
Joined
Feb 1, 2008
Messages
18,310
Location
Kiel/Wherever, Germany
Car(s)
'19 BMW M240i
Aha. You're looking for a column `filename`, but there is no such column.

Hint: echo mysql_error(); after the request (when doing text output, not binary data).
 

Jens

Well-Known Member
Joined
Jul 12, 2009
Messages
1,867
Location
Belgium
Car(s)
BMW 116d, W124 250D
So, I need to replace 'filename' with 'image' or create a new column 'filename' and enter the filename, which is test in this case.
Excuse me for the rather noob-ish question, but I don't have a lot of experience with MySQL :)
 

narf

Sgt. Maj. Buzzkill
DONOR
Joined
Feb 1, 2008
Messages
18,310
Location
Kiel/Wherever, Germany
Car(s)
'19 BMW M240i
Well, you appear to be looking for something containing foo.jpg, so just enter the column name that contains such values. Replacing it with image won't help, because then you would be matching on the binary large object.

Considering you already have a primary key called id, why not address the rows by that?
 

Jens

Well-Known Member
Joined
Jul 12, 2009
Messages
1,867
Location
Belgium
Car(s)
BMW 116d, W124 250D
I've decided it will be much faster and easier to just save the image in my directory (not in a database) and only put the name in the database and then just echo that name and add .jpg
I suppose that'll work.
 

Viper007Bond

Chicken Nugget Connoisseur
STAFF MEMBER
Joined
Sep 21, 2003
Messages
31,046
Location
Portland, Oregon
Car(s)
2008 Dodge Viper, 2006 MB CLS55 AMG
I've decided it will be much faster and easier to just save the image in my directory (not in a database) and only put the name in the database and then just echo that name and add .jpg
I suppose that'll work.
^ Correct way to do it. Serving an image from the filesystem is a million times faster than serving it from the database and it scales a LOT better.
 

PacketCollision

Server Admin/Crasher
STAFF MEMBER
Joined
Feb 19, 2007
Messages
1,297
Location
San Francisco
Car(s)
generator on wheels
I've decided it will be much faster and easier to just save the image in my directory (not in a database) and only put the name in the database and then just echo that name and add .jpg
I suppose that'll work.
Good. Putting images in the database is never the Right Way To Do It. Also, be careful with the way you access data, someone could create a malicious request and delete of your database content. Make sure you escape things with mysql_real_escape_string.
 

Hidden_Hunter

Needs more IceBone
Joined
Oct 7, 2006
Messages
8,614
Location
Melbourne, Australia
Car(s)
'13 BMW 125i, '26 Buick Standard Six
I can't actually think of (m)any scenario's where you would actually want to use blob's in a db to store images
 
Top