Page 1 of 2

Retrieving Binary Data and Displaying

Posted: Fri Jul 02, 2004 6:38 pm
by WelshApple
Hi

I can put image files into the MySQL database, but I'm stuck on retrieving the image and displaying it with PHP. I get this far.........

Code: Select all

<?php
$link = mysql_connect('localhost', USERNAME, PASSWORD);
mysql_select_db('DB');
$query = "SELECT image FROM table WHERE id='1'";
$result = mysql_query($query, $link);
?>
............what comes next?

Posted: Fri Jul 02, 2004 6:39 pm
by feyd
drop some headers.. like "Content-type: image/jpeg" then echo the image data.

Not sure

Posted: Fri Jul 02, 2004 6:44 pm
by WelshApple
Thank for replying.....

I'm not sure where I would be using the headers e.g.

Code: Select all

<?php
$result = mysql_query($query, $link);
$image = mysql_fetch_object($result, $headers);
?>
Sorry for sounding dumb.

Posted: Fri Jul 02, 2004 6:50 pm
by feyd
well.. you need to have a way of determining what the binary data is (mime-type wise), it's best to store some marker, either a number (referencing another table with content-types) or something..

Posted: Fri Jul 02, 2004 6:51 pm
by WelshApple
So there's no simple way of retrieving an image file and using it to display it's image data?

Posted: Fri Jul 02, 2004 6:57 pm
by feyd
you need to know which mime-type the data is in, in order to send it to the browser so it can render it properly.. most browser don't just figure out what you send them in byte streams...

Posted: Fri Jul 02, 2004 6:59 pm
by feyd
all you need to do, is mark in the database, if this image is a jpeg, gif, png, whatever.. then send the proper mime-type to the browser just before the binary data. Otherwise, the browser will likely just write all the binary data as text, which will fill the screen with garbage..

Posted: Fri Jul 02, 2004 7:00 pm
by WelshApple
Ok, so what code would I use supposing the image file stored is a normal .jpg?

Posted: Fri Jul 02, 2004 7:02 pm
by feyd

Code: Select all

<?php

// the code you already have here...

header('Content-type: image/jpeg');

list($data) = mysql_fetch_row($result);
echo $data;

?>

Posted: Fri Jul 02, 2004 7:12 pm
by WelshApple
This is what I just wrote and tested, but output was nothing :-(

Code: Select all

<?php
$link = mysql_connect('localhost', 'USERNAME', 'PASSWORD');
mysql_select_db('DATABASE');
$query = "SELECT image FROM test WHERE id='1'";
$result = mysql_query($query, $link);
header('Content-type: image/jpeg');
list($data) = mysql_fetch_row($result);
print $data;

?>
Any suggestions?

Posted: Fri Jul 02, 2004 7:19 pm
by feyd
toss "or die(mysql_error())" after mysql_connect(), mysql_select_db(), mysql_query().. you may want to put in a check to see if something was returned from the query.. so calling mysql_num_rows($result) somewhere in there (after the query) checking it's return would be a good thing to add too...

Posted: Fri Jul 02, 2004 7:27 pm
by WelshApple
hmmmmm, interesting........

Connection is ok. mysql_num_rows() is 1. The window title is image.jpg. I can right-click mouse and download image.jpg but the downloaded file is not recognisable???

Posted: Fri Jul 02, 2004 7:38 pm
by markl999
Works fine here if i mysql_escape_string() the image before inserting it into the db (i have magic_quotes Off). I suspect magic_quotes is your problem </guess>.

Posted: Fri Jul 02, 2004 7:40 pm
by WelshApple
so how would i go about turning magic quotes off to try?

thanks

Posted: Fri Jul 02, 2004 7:43 pm
by WaldoMonster
Now it must be working.

Code: Select all

<?php
$link = mysql_connect('localhost', 'USERNAME', 'PASSWORD'); 
mysql_select_db('DATABASE'); 
$query = "SELECT image FROM test WHERE id='1'"; 
$result = mysql_query($query); // removed $link 
header('Content-type: image/jpeg'); 
list($data) = mysql_fetch_row($result); 

header('Content-type: image/jpeg'); //added header
print $data;
mysql_close($link); // close connection
?>
It is also posible to use mysql_pconnect() instead of mysql_connect().
Then you don't have to close the connection.