Retrieving an image from a blob field in a mysql 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
tenacious-dee
Forum Newbie
Posts: 19
Joined: Tue Jan 20, 2009 8:44 am

Retrieving an image from a blob field in a mysql database

Post by tenacious-dee »

I've been having lots of problems trying to get this piece of code reading and image from my mysql database

This is the php code retrieving the image saved in retAnStad.php:

Code: Select all

 
<?php
    $username = "root";
    $password = "********!";
    $host = "localhost";
    $database = "anml";
                
    @mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());
            
    @mysql_select_db($database) or die("Can not select the database: ".mysql_error());
                
                
    $query = mysql_query("SELECT image FROM tblretaildining WHERE retailDining_id='1'");
    $row = mysql_fetch_array($query);
    //$content = $row['image'];
    
    header('Content-type: image/jpeg');
    echo $row['image'];
?>
 
and then in the html code i'm trying to retrieve the output from that file using an <img> tag:

<img src="retAnStad.php" alt="An Stad Picture"/>

could anyone offer some advise as to where i might be going wrong... this has been bugging me for a week now!

thanks so much
dee
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Retrieving an image from a blob field in a mysql database

Post by mattpointblank »

Does your query actually work? Put it directly into phpMyAdmin or something - do you see the results you'd expect?

Also, try modifying this line:

$query = mysql_query("SELECT image FROM tblretaildining WHERE retailDining_id='1'");

to this:

$query = mysql_query("SELECT image FROM tblretaildining WHERE retailDining_id='1'") or die (mysql_error();

This'll output any errors encountered by the query.
tenacious-dee
Forum Newbie
Posts: 19
Joined: Tue Jan 20, 2009 8:44 am

Re: Retrieving an image from a blob field in a mysql database

Post by tenacious-dee »

Thanks for that!

Yeah I just checked the query out there in navicat and it pulls the image from that row alright.. but still no joy in the browser
User avatar
sergio-pro
Forum Commoner
Posts: 88
Joined: Sat Dec 27, 2008 12:26 pm

Re: Retrieving an image from a blob field in a mysql database

Post by sergio-pro »

try opening retAnStad.php page directly from browser. You can see error messages then, if there are any.
tenacious-dee
Forum Newbie
Posts: 19
Joined: Tue Jan 20, 2009 8:44 am

Re: Retrieving an image from a blob field in a mysql database

Post by tenacious-dee »

All sorted now... rookie error

I was just referencing it wrong in the image source!

Apolocies for the stupidity! And thanks for the help!
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: Retrieving an image from a blob field in a mysql database

Post by Burrito »

please use PHP tags when posting code in the forum.
Post Reply