Why can't I display a image stored as blob from MySQL in PHP ?

I have a "image" table structure like this :
(image_id int
image_type varchar(50)
image blob
image_size bigint
image_name varchar(255)
image_date datetime)

and I've a image.php scripted as :
<html>
<head>
<title>My Image</title>
</head>
<body>
<H1>Look at this:</H1>
<IMG SRC="load_image.php">
</body>
</html>

And in the load_image.php, here's the script:
<?
$db = @mysql_connect(localhost, "root", "root") or die("Can't connect to server.");

@mysql_select_db("dating", $db) or die("Can't select database.");

//Create and execute the SQL statement:

$get_image = "select image, image_type from image where image_id = 8";

$get_image_result = @mysql_query($get_image)  or die("Couldn't get image.");

//Get the individual elements out of the result set:

$binary_junk = @mysql_result ($get_image_result,0,"image");

$filetype = @mysql_result ($get_image_result,0,"image_type");

header("Content-type: $filetype");
echo "$binary_junk";
?>

WHEN THE image.php IS LOAD IN BROWSER, THE IMAGE COULD NOT DISPLAYED, WHY ? I?M SURE THE BLOB FIELD IS FILLED UP WITH CORRECT DATA. CAN ANYBODY TELL ME WHAT's GOING WRONG OR WHERE CAN I FIND A SCRIPT THAT INSERT IMAGE AND SELECT IMAGE THAT WORKS.
edmondlimAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CyberGhostCommented:
Actually I don't understand why are you storing image in BLOB database. You could just put it on server and then load it from there. Why getting it from DB?

But this code will maybe help you a little:

$result = mysql_query("SELECT image, image_type from image where image_id = 8");

while ($row = mysql_fetch_array($result))
{
 $binary_junk = $row["image"];
 $filetype = $row["image_type"];
}
mysql_free_result($result);

header("Content-type: $filetype");
echo $binary_junk;


... that is a well-known statement that works (as far as I know). Or check if you have good field names written and can connect to DB (there is no OR DIE statement when connecting to DB in your code). These are the most frequent mistakes people do working with databases.

regards
0
CyberGhostCommented:
... sorry, my mistake. I have missed that u have OR DIE statement in there. Well... just check your table field names or try the code above...
0
edmondlimAuthor Commented:
Sorry, it still doesn't display the image. As far as I can see, your different is just that putting the contents in array.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

CyberGhostCommented:
Well... then the problem is maybe not in the PHP code.


Maybe you can try to use this:

<html>
<head>
<title>My Image</title>
</head>
<body>
<H1>Look at this:</H1>
<IMG SRC="<?php include("load_image.php"); ?>">
</body>
</html>

... instead of using that page as a SRC parameter.
0
CyberGhostCommented:
... anyway there is something I've missed again. You are trying to put the header informations in BODY part, where no headers can be set. These must be set before generating any HTML code. So you maybe could use that code included on the top of your page and then just read the value of $binary_junk right as IMG SRC parameter. But that HTML have to be PHP of course.
0
foreverfreshCommented:
hi,

I don't know what is your uploader code.
please try yhis files

//---------------------------
// upload.php
<html>
<body>
<?
  if(strlen($img_name))
  {
    $SQLdbi = mysql_connect("localhost","root","");
    mysql_select_db("Mydb", $SQLdbi);    
    $imgsize = filesize($img);
    $imgdata = addslashes(fread(fopen($img,"rb"), $imgsize));
    $SQLquery  ="INSERT INTO image (id,image_type,image,image_size,image_name,image_date)"
              ."VALUES ('','$img_type','$imgdata','$imgsize','$img_name',"
              ."'".date("Y-m-d H:i:s")."')";
    mysql_query($SQLquery,$SQLdbi);
  }
?>
<form action="" method="post" enctype="multipart/form-data" name="form1">
<input name="img" type="file" id="img"> <input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
//--------------------------------------------

//--------------------------------------------
// showimage.php
<?
  $SQLdbi = mysql_connect("localhost","root","");
  mysql_select_db("Mydb", $SQLdbi);
  $SQLquery  ="SELECT * FROM image WHERE id='1'";
  $result= mysql_query($SQLquery,$SQLdbi);
  $row=mysql_fetch_array($result);
  header("Content-type: $row[image_type]");
  echo "$row[image]";
  mysql_close($SQLdbi);
?>
//---------------------
0
edmondlimAuthor Commented:
"foreverfresh", your solution doesn't work either. the upload.php managed to store the image uploaded but the showimage.php shows nothing. I wonder is there anything I've to configure in the php.ini file or in the Apache server ?
0
foreverfreshCommented:
did you change id='1' to id=??? in showimage.php
//-------
 $SQLquery  ="SELECT * FROM image WHERE id='1'";
//-------
  or you can use
//
  $SQLquery  ="SELECT * FROM image WHERE id='$imid'";

http://yourserver/showimage.php?imid=5

this code works for me :)

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bobsledbobCommented:

edmondlim, two thoughts:

1)  just to be sure your 'filetype' field is actually a mime type?  ie. you're storing image/gif in there instead of just gif?  Sending the header Content-type: gif  won't help things out.  

2)  There's some issues around progressive jpegs (mime type image/pjpeg).  You shouldn't have a problem with a newer browser I'm guessing, but for older browsers sending mime type image/jpeg whilst really serving a pjpeg will break.  The opposite is true too.  Like I said though, I think most browswers these days can mix and match progressive and normal jpeg files, regardless of the mime-type the webserver sends them.  Also, sending image/jpg is bad news too, which I've seen some code before do.
0
grantwongCommented:
edmondlim,

What happens if you go to http://yoursever.com/load_image.php

You should see an image.  If you don't, you should be able to diagnose the error from there.  Generally when something like this doesn't work out it is due to some output that is happening.

To ensure that no output is happening before your call to header(), and your echo of the blob, you can do:

ob_end_clean();
ob_start();

to clear the output buffer.

After you echo, you should do a return; or exit;

---
ob_end_clean();
ob_start();
header($filetype);
echo $image;
return;

Hope this helps.

0
edmondlimAuthor Commented:
It's works now
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.