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:
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:
 
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
 
This is what it looks like :)
database.jpg
 
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).
 
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 :)
 
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?
 
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.
 
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.
 
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.
 
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