Page 1 of 1

Retrieving an image from a blob field in a mysql database

Posted: Tue Jan 20, 2009 8:48 am
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

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

Posted: Tue Jan 20, 2009 8:53 am
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.

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

Posted: Tue Jan 20, 2009 8:57 am
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

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

Posted: Tue Jan 20, 2009 9:01 am
by sergio-pro
try opening retAnStad.php page directly from browser. You can see error messages then, if there are any.

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

Posted: Tue Jan 20, 2009 9:09 am
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!

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

Posted: Tue Jan 20, 2009 9:53 am
by Burrito
please use PHP tags when posting code in the forum.