Jump to content


Photo

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


  • Please log in to reply
4 replies to this topic

#1 jacobs

jacobs
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 27 March 2006 - 07:09 AM

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;

?>

#2 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 27 March 2006 - 08:27 AM

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:
Header( "Content-type: $type");
echo $data;

type:

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

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.


"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#3 jacobs

jacobs
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 27 March 2006 - 08:42 AM

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

#4 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 27 March 2006 - 09:03 AM

[!--quoteo(post=358807:date=Mar 27 2006, 09:42 AM:name=jacobs)--][div class=\'quotetop\']QUOTE(jacobs @ Mar 27 2006, 09:42 AM) View Post[/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:

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

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.
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#5 jacobs

jacobs
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 28 March 2006 - 01:22 AM

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) View Post[/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:

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

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]






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users