Faqts : Computers : Databases : MySQL : Language and Syntax : Field Types : Blob

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

397 of 467 people (85%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How can I store a picture in MySQL?
Should I use a blob to store an image in the database?
Should I store images in a database or as files?
How can I get an image (or blob field) back out of the database and into a file?

May 18th, 2000 06:11
Nathan Wallace, Sasha Pachev


You should use BLOB type. Choose a subtype according to the sizes of
pictures that you intend to store.

If you are using 3.23.xx, you should use load_file(...) function for
storing images.

To get the image back out into a file try "select ... into outfile".

In general though, it's really not worth doing this for performance
reasons... in fact there aren't many reasons to do this at all. 

The best way to store these images is on your file system, and only
storing a path to them in the database, we do this in our web site
(http://neopets.com) when kids upload images and text onto the site. 
Apache is MUCH better at caching and retrieving files for web use than
mySQL is, or will ever need to be (well unless you can prove me wrong
:).

Consider the two senarious:

to show db images:
- the image is first read from the data file into MySQL record buffer
- from the record buffer the image goes into the socket
- the application reads it from the socket into its internal buffer
- the contents of the internal buffer are forwarded to the user

file system, not even super highly optimized:
- the image is read from the data file into internal buffer of the web
server.
- the contents of the internal buffer are forwarded to the user