It seems that the latest and greatest use for databases is storing large amounts of binary data, known as BLOB's. These BLOB's can store just about any type of data imaginable, including MS Word documents, GIF/JPEG images, PDF files, MP3's, etc. In this article Mitchell shows us how to create a binary file repository using PHP and MySQL that can store and retrieve several different file types.
Blobbing Data With PHP and MySQL - Creating the database (Page 2 of 7 )
Our document repository will use one database containing one table to store its data. Using the MySQL console application (c:\mysql\bin\mysql.exe for Win32 or /usr/bin/mysql for *nix users), create our database and switch to it with the following commands:
create database myFiles;
use myFiles;
Now let's create the table that will actually store our BLOB's as part of our myFiles database:
create table myBlobs
(
blobId int auto_increment not null,
blobTitle varchar(50),
blobData longblob,
blobType varchar(50),
primary key(blobId),
unique id(blobId)
);
We now have one database named myFiles, which contains one table named myBlobs. The details of each field in the myBlobs table are shown below:
blobId: An integer that will provide us with a unique numerical identifier for each blob (ie 1, 2, 3, etc). It is incremented and tracked internally by MySQL whenever we add a new record.
blobTitle: A description of each blob in the table, for example "My word document about golf", or "Picture of the sky". Servers no real purpose, but will come in handy when we are viewing our files in through a web page later on.
blobData: A binary field that will hold the contents of each file that we wish to store in the database. In our table, we have used the longblob data type, which can hold up to 4,294,967,295 characters. Other binary field types include mediumblob (which can hold up to 16,777,215 bytes), blob (which can hold up to 65535 characters), and tinyblob (which can hold up to 255 characters).
blobType: As you will see shortly, each different file type (such as .doc, .gif, .pdf, etc) has its own unique content type. When a file is uploaded via a web browser to a web server (which we will look at shortly), the browser cleverly passes that files type as part of its headers. We will be storing this type in the table so that we can choose to view only specific types of files from our myBlobs table. Content types are simple strings. The content type for a MS word document is "application/msword", the content type for a GIF image is "image/gif", etc.