Jump to content

retrieving BLOB fields


danrah

Recommended Posts

I have a BLOB field in MySQL, InnoDB which  stores jpeg images.
when i want to retrieve data from my code using:
echo $row['image']
I can see only the binary codes.
what should i do?
(Images are inserted properly. when i retrieve them with mysql query browser the images are not corrupted)
Link to comment
Share on other sites

you can not directly display images stored in a db in a web page like that.  what you have to do is put an image tag like this in the web page:
[code]<img src="getimage.php?id=????">[/code]
the ???? part above would be replaced by the primary key of the row # in the db that the image is stored in.  If you need to determine that at run time, then you have to code it like this:
[code]<img src="getimage.php?id=<?php echo $rownum; ?>">[/code]
--notice the php code nested in the img tag.  $rownum would have to be set somewhere above in the code.
--the getimage.php contains code that gets the url passed rownum, opens the db, retrieves the correct row, then does this:
[code]header("Content-type:  image/jpeg");
echo $dbimagefield; //now we echo it[/code]
--getimage.php must not output anything except the 2 statements above, even a single excess space before the opening php start tags in that code will cause it to fail.

--If you store graphics right in the db itself, you have to go through all kinds of extra hoops to display them on a web page.  As well, all the extra hits on the db will slow down the page.  That's why it's almost always better to store just the file name of the graphic in the db, and store the graphic as just a normal graphic on the server.
Link to comment
Share on other sites

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.