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
Post
by WelshApple » Fri Jul 02, 2004 6:38 pm
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?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Jul 02, 2004 6:39 pm
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
Post
by WelshApple » Fri Jul 02, 2004 6:44 pm
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Jul 02, 2004 6:50 pm
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 » Fri Jul 02, 2004 6:51 pm
So there's no simple way of retrieving an image file and using it to display it's image data?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Jul 02, 2004 6:57 pm
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...
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Jul 02, 2004 6:59 pm
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 » Fri Jul 02, 2004 7:00 pm
Ok, so what code would I use supposing the image file stored is a normal .jpg?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Jul 02, 2004 7:02 pm
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 » Fri Jul 02, 2004 7:12 pm
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?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Jul 02, 2004 7:19 pm
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 » Fri Jul 02, 2004 7:27 pm
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???
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Fri Jul 02, 2004 7:38 pm
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 » Fri Jul 02, 2004 7:40 pm
so how would i go about turning magic quotes off to try?
thanks
WaldoMonster
Forum Contributor
Posts: 225 Joined: Mon Apr 19, 2004 6:19 pm
Contact:
Post
by WaldoMonster » Fri Jul 02, 2004 7:43 pm
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.