Jump to content

Archived

This topic is now archived and is closed to further replies.

jacobs

Help!!! How to show single image from mysql database

Recommended Posts

Hello everyone, I been trying to load an image from a mysql database using php. The code that I have works fine but once I change background color of the page or add an html table, the jpg picture won't appear and instead it appear as character like ÿØÿàJFIFHHÿÛC Could it be because of configuration in php.ini? Any suggestion I would really appreciate it. I am using iis for the webserver. I really appreciate your help. Thanks in advance


Here are the code that I have


session_start();

$id = $_POST['id'];
// you may have to modify login information for your database server:
@MYSQL_CONNECT("localhost","xxxxx","xxxxx");

@mysql_select_db("wip");

$query = "select content,type from data where fileid = '$id'";
$result = @MYSQL_QUERY($query);

$data = @MYSQL_RESULT($result,0,"content");
$type = @MYSQL_RESULT($result,0,"type");
Header( "Content-type: $type");


echo $data;

?>

Share this post


Link to post
Share on other sites
hi
there are a couple of things to check.

1, above all the random characters, can you see any error messages?
2, before this section:
[code]
Header( "Content-type: $type");
echo $data;
[/code]

type:

[code]
echo 'results: '.mysql_num_rows($result);
echo '<br>';
echo 'type: '.$type;
exit;
[/code]

you're looking for a non-zero number for result, and a correct MIME type (if $type is set from when the file was first uploaded from $_FILES['myfile']['type'] then it should be fine.

Share this post


Link to post
Share on other sites
Hello, thanks for the reply. Well above the random character, I did not see any error. Once I removed the changes I made the picture will be shown on the screen but in a really simple page format (white background and the picture itself). Do you think I have to encode the image ( I am using longblob type - I am saving the image into mysql database)? Do you have a simple example of how to load data from mysql?




thanks in advance

Share this post


Link to post
Share on other sites
[!--quoteo(post=358807:date=Mar 27 2006, 09:42 AM:name=jacobs)--][div class=\'quotetop\']QUOTE(jacobs @ Mar 27 2006, 09:42 AM) [snapback]358807[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hello, thanks for the reply. Well above the random character, I did not see any error. Once I removed the changes I made the picture will be shown on the screen but in a really simple page format (white background and the picture itself). Do you think I have to encode the image ( I am using longblob type - I am saving the image into mysql database)? Do you have a simple example of how to load data from mysql?
thanks in advance
[/quote]

youre pretty much doing it right, but remember that this script should only be used by calling it from elsewhere, and should not be embedded into your main scripts. so if the file you have got here is, eg, image.php, then you will need to call it via:

[code]
<img src='image.php?id=3' />
[/code]

if you have this script inside a page that also draws other items, notably Tables, etc, then it will not work as Headers need to be sent BEFORE any other content goes to the browser. change $_POST to $_GET in your file, and call it as i shown you above.

Share this post


Link to post
Share on other sites
redbullmarky thank you for the help. Problem is now solved. Really appreciate your help


[!--quoteo(post=358810:date=Mar 27 2006, 04:03 AM:name=redbullmarky)--][div class=\'quotetop\']QUOTE(redbullmarky @ Mar 27 2006, 04:03 AM) [snapback]358810[/snapback][/div][div class=\'quotemain\'][!--quotec--]
youre pretty much doing it right, but remember that this script should only be used by calling it from elsewhere, and should not be embedded into your main scripts. so if the file you have got here is, eg, image.php, then you will need to call it via:

[code]
<img src='image.php?id=3' />
[/code]

if you have this script inside a page that also draws other items, notably Tables, etc, then it will not work as Headers need to be sent BEFORE any other content goes to the browser. change $_POST to $_GET in your file, and call it as i shown you above.
[/quote]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.