Link to home
Start Free TrialLog in
Avatar of edmondlim
edmondlim

asked on

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.
Avatar of CyberGhost
CyberGhost
Flag of Czechia image

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
... 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...
Avatar of edmondlim
edmondlim

ASKER

Sorry, it still doesn't display the image. As far as I can see, your different is just that putting the contents in array.
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.
... 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.
Avatar of foreverfresh
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);
?>
//---------------------
"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 ?
ASKER CERTIFIED SOLUTION
Avatar of foreverfresh
foreverfresh
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

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

It's works now