I want to display an image from database so i am using this script:

<?php
header("Content-type: image/jpeg");
$username = "root";
$password = "root";
$host = "localhost";
$database = "binary";
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());
$id = 3;
$rows = mysql_query("SELECT image FROM imagetable WHERE id='".$id."'");
while($row=mysql_fetch_row($rows))
    {
        header('Content-type: image/jpg');
        print "$row[0]" ;
    }

?>

but when i open this file in the browser it says:
error10

When I comment out the line
header('Content-type: image/jpg');
then the error goes and the image bytes are displayed.

Please help me. Thanks

Hi,

Maybe the problem is because there are 2 header()
header("Content-type: image/jpeg");
and
header('Content-type: image/jpg');

$rows = mysql_query("SELECT image FROM imagetable WHERE id='".$id."'");
while($row=mysql_fetch_row($rows))
{
header('Content-type: image/jpg');
print "$row[0]" ;
}

remove the header there, are you pulling more than 1 image? you can only display 1 image on a php page

$rows = mysql_query("SELECT image FROM imagetable WHERE id='".$id."' LIMIT 1");
$row=mysql_fetch_row($rows);
print "$row[0]";

If you want to show several images on one page create a separate php file called image.php and use <img src='image.php?id=324'/> and you can display many images on the same page that way, only 1 image can be outputted from raw data in a http request

thank you MarPlo and Biiim :).

I tried to change the code according to you both but still same error is displayed.

If you have any working code for fetching the image for database it shall be of great help to me.

annoyingly I don't have any database stored images, i might have a go at some point i was worried about the database size getting large - i just save it as a file and its url in a database

Check that there is no whitespace at the top of the page, other than that i can only imagine that the image data is being corrupted along the line - maybe mysql is saving it in a odd format. I checked through it a couple times and dont see anything wrong with that code, especially if you can output it as text and see no php errors

thanks Biiim for the nice suggestion.......i will try saving the image as file and then save its url in database ...:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.