show image from blob mysql

Hi I have a image table in my database. These are stored as blob along with details such as image type & name.

I am having a problem showing the image, all I get is a white box with a red cross in it.


include '../connection.php';

$ID = $_GET['id'];

$query = "SELECT * FROM `images` WHERE `image_id` = '$ID'";

$row = mysql_fetch_array($result);

$image = $row['image'];
$image_type= $row['image_type'];   

header("Content-type: $image_type");
print $image; 





Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Well here is a short answer.

include '../connection.php';
$id = (int)$_GET['id'];
$query = "SELECT * FROM `images` WHERE `image_id` = '$id'";

$row = mysql_fetch_array($result);

$image = $row['image'];
$image_type= $row['image_type'];
$size = $row['image_size'];
/* list($image, $image_type, $size) = array(
$ext = explode('/', $image_type);
$name = $id . '.' . $ext[1]; 

header("Content-type: $image_type");
header("Content-length: $size");
header("Content-Disposition: attachment; filename=$name");

print $image;     

Check your blobtype to be a least MEDIUMBLOB which is able to store data up to 16M

Method 2

To debug this, I’d suggest commenting-out the Content-type header line, then hitting the url directly in the browser. This will allow you to see any errors, warnings or notices that PHP might be emitting.

Method 3

Couple of things to try

  1. maybe something is failing and it is returning an error msg in html instead of the expected image
  2. is the content_type stored correctly in the database or are you just storing the file extension of the image

content-type should look something like this


Method 4

That looks like it might work, what’s going wrong?

Try to specify the fields explicitly:

 SELECT image, image_type FROM ...

What happens when you run the query from the database?

Are you loading the image like:

<img src="image.php?id=12">

Or do you load the PHP as its own page?

Method 5

maybe you could try

$row = mysql_fetch_assoc($result);

instead of

$row = mysql_fetch_array($result);

Method 6

You said in a comment that the table initially said “[BLOB – 64.0 KiB]” but you then changed it to “MEDIUMBLOB”. This will expand the size that you can store, but all of your existing data will still be truncated to 64KiB.

Make sure that the field type you use is large enough to store the data you want to store (16mb in a MEDIUMBLOB or ~4gb in a LONGBLOB I’m pretty sure) and then re-insert all of your data.

Other than the security problems mentioned, I don’t see why the code shouldn’t work other than the database problem.

Method 7

Or if you don’t want to create a separate php file, you can inline it

// retrieve blob into $img
?><img src='data:image/png;base64,<?php echo base64_encode( $img );?>' alt='Image <?php echo $id;?>'>

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x