Retrieving Binary Data and Displaying

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

Moderator: General Moderators

WelshApple
Forum Newbie
Posts: 9
Joined: Fri May 21, 2004 12:07 pm
Location: Wales-UK

Retrieving Binary Data and Displaying

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

drop some headers.. like "Content-type: image/jpeg" then echo the image data.
WelshApple
Forum Newbie
Posts: 9
Joined: Fri May 21, 2004 12:07 pm
Location: Wales-UK

Not sure

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
WelshApple
Forum Newbie
Posts: 9
Joined: Fri May 21, 2004 12:07 pm
Location: Wales-UK

Post by WelshApple »

So there's no simple way of retrieving an image file and using it to display it's image data?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
WelshApple
Forum Newbie
Posts: 9
Joined: Fri May 21, 2004 12:07 pm
Location: Wales-UK

Post by WelshApple »

Ok, so what code would I use supposing the image file stored is a normal .jpg?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

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

?>
WelshApple
Forum Newbie
Posts: 9
Joined: Fri May 21, 2004 12:07 pm
Location: Wales-UK

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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...
WelshApple
Forum Newbie
Posts: 9
Joined: Fri May 21, 2004 12:07 pm
Location: Wales-UK

Post 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???
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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>.
WelshApple
Forum Newbie
Posts: 9
Joined: Fri May 21, 2004 12:07 pm
Location: Wales-UK

Post by WelshApple »

so how would i go about turning magic quotes off to try?

thanks
User avatar
WaldoMonster
Forum Contributor
Posts: 225
Joined: Mon Apr 19, 2004 6:19 pm
Contact:

Post 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.
Last edited by WaldoMonster on Fri Jul 02, 2004 7:47 pm, edited 2 times in total.
Post Reply