Retreiving images from database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gleb
Forum Newbie
Posts: 2
Joined: Mon Apr 13, 2009 6:36 am

Retreiving images from database

Post by gleb »

This is my first attempt at retrieving an image from a database

Code: Select all

<?
header("Content-type: image/jpeg");
mysql_connect("localhost","root","xxxxx"); 
mysql_select_db("test");
$mysql ="SELECT imgdata".
    "FROM pix ".
    "WHERE pid = 14  "; 
if ($row = mysql_fetch_assoc($mysql)) {
        $bytes = $row[imgdata];}
print $bytes;
?>
It prints the address of the page i.e. http://localhost/index.php
It makes the page: title index.php (JPEG image)

Can anyone explain what is going on?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Retreiving images from database

Post by Christopher »

Is there data in $bytes ?
(#10850)
gleb
Forum Newbie
Posts: 2
Joined: Mon Apr 13, 2009 6:36 am

Re: Retreiving images from database

Post by gleb »

Only what's put there by $bytes = $row[imgdata];
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Re: Retreiving images from database

Post by n00b Saibot »

I think you forgot to do a mysql_query() first....

Code: Select all

 
$mysql = "SELECT imgdata".
              "FROM pix ".
              "WHERE pid = 14  "; 
$result = mysql_query($mysql);
if ($row = mysql_fetch_assoc($result)) {
        $bytes = $row[imgdata];
}
print $bytes;
Post Reply