PhpRiot
Download This Article
Download this article in PDF format with all listings and files.

Price: $5.00 AUD
(Approx. $4.10 USD)

More information
Related Books
PHP and MySQL Web Development (4th Edition) (Developer's Library)

PHP and MySQL Web Development (4th Edition) (Developer's Library)

PHP and MySQL are popular open-source technologies that are ideal for quickly developing...

PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide

PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide

It hasn't taken Web developers long to discover that when it comes to creating dynamic,...
Browse Articles
Ajax (4), APC (1), CAPTCHA (1), CSS (3), Debugging (1), File Upload (1), Google (3), Google Maps (2), JavaScript (12), JSON (2), MVC (1), MySQL (7), onbeforeunload (1), OOP (1), PHP (28), PhpDoc (1), PostgreSQL (6), Prototype (11), Reflection (1), RFC 1867 (1), Robots (1), Scriptaculous (1), SEO (1), Sessions (1), SimpleXML (1), Smarty (5), SOAP (1), SPL (1), Templates (2), W3C (1), XHTML (1), Zend Framework (1), Zend_Search_Lucene (1)

PhpRiot Newsletter
Your Email Address:

Storing Images In MySQL

Creating An Image Table

Of course we need to create a table to store our images in. Here is a simple table:

Listing 2 listing-2.sql
create table testblob (
    image_id        tinyint(3)  not null default '0',
    image_type      varchar(25) not null default '',
    image           blob        not null,
    image_size      varchar(25) not null default '',
    image_ctgy      varchar(25) not null default '',
    image_name      varchar(50) not null default ''
);

Ok, nothing dramatic here, but let’s break it down.

First we have an id field, this should be an auto incremented field so we avoid confusion when selecting.

Next we have the image type (mime type). This is handy as we can use PHP to determine this and use it for sending the correct headers when we need to display the image.

Next we have the image_blob field. This is the field that will contain the actual image data.

Then we have the image size as determined by getimagesize().

Then we have a category for our images, you may wish to have have a second table of categories to populate your form dropdown for this. But for our purposes we will only be using three.

Finally, we have the image name. You can add more items to this table such as exif info and descriptions, but for our purposes we will keep it simple.

In This Article


Tagged in ,

Article History

May 30, 2005
Initial article version
Jan 18, 2008
Updated listing 5 to not include non-existent conf.php