0

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

Edited by Dani: Formatting fixed

3
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by kratijain
2

Hi,

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

0

$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

Edited by Biiim

0

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.

0

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

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.