20120425

Why a Database?


If you’re developing a PHP application using content / data, you’re likely to need a database. Even for  something small, like a personal blog, you want to think hard about the advantages of using a database instead of static pages or included text files.

Maintainability and scalability

Having PHP assemble your pages on the fly from a template and a database is an addictive experience. Once you enjoy it, you’ll never go back to managing a static HTML site of any size. For the effort of programming one page, you can produce an infinite number of uniform pages. Change one, and you’ve changed them all.

There are now web sites with hundreds of thousands of separate pages — you can rest assured that no one is maintaining them all by hand. If you have a web site that may eventually grow to more than a few dozen pages, you should think about moving to a database sooner rather than later.

Portability

Because a database is an application rather than a part of the operating system, you can easily transfer its structure and contents from one machine to another or (in certain cases) even from one platform to another. This is especially valuable for contractors, who may develop a project without being able to control the environment in which it will eventually be deployed — they can deliver a package of PHP plus a MySQL database schema dump.

Avoiding awkward programming

Certain things can be done with PHP but probably shouldn’t, because they entail ugly or risky programming moves. Say that you happen to be the commander of the "Airbus 787"  and are keeping a captain’s log. Each log entry is contained in a text file identified by its unique stardate, which is plugged into a template by PHP — but hey, you’re a busy spaceman with whole galaxies to explore; you don’t always have time to write in your log every day. You want to put automatically generated Next and Previous links on each page for those who wish to read in straight chronological order. It’s pretty easy to use PHP to find the previous stardated entry, but any attempt to locate the next entry can quickly become an infinite loop — because it’s easier to prove something does exist than that it doesn’t. On the other hand, if you put your log data in a database, the whole job becomes trivial.

The database will tell you which is the latest entry at any given moment. There are other types of programming tasks that a database is highly optimized to do, and given the option, you should take advantage of it to perform these chores. For instance, you should avoid sorting data sets on the PHP side in favor of writing queries so the data is returned presorted. Searching Although it’s possible to search multiple text files for strings (especially on Unix platforms), it’s not something most web developers will want to do often. After you search a few hundred files, the task becomes slow and hard to manage.

In some cases, information attains value only when put into a searchable database. For instance, relatively few people would want to read a long text list of movie directors and their films, but many might occasionally want to search a database of that information. You could argue that it’s the searchability, as much as the information itself, that creates the value here.

No comments: