Jump to content

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;

?>
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.

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
[!--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.
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]

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.