[thelist] Database vs. Flat file vs. XML

Simon Willison simon at incutio.com
Fri May 24 04:09:00 CDT 2002


At 12:24 23/05/02 -0700, Burhan Khalid wrote:
>--
>[ Picked text/plain from multipart/alternative ]
>Hello Listees :
>
>     All this talk about databasing has me thinking, what the advantages
> of databases over say an XML file or a flat file for simple projects. I
> had a collection of quotes in a database, which I moved to a flat file
> (since it was, imo, overkill). When does data require the use of a
> database? Does it have to do more with the content, or the
> application?  In your experience(s), have you ever had to move stuff away
> from database to a simpler system(s)? I know lots of people go the other
> route, but when would it be better to "downgrade"? Short of moving to a
> host that doesn't have your favorite database, or for backup, are there
> any other reasons to go to flat-filing?

Actually I used to use databases for almost all of my PHP work but I have
recently switched to using flat text files for smaller projects. The
practical reason for this is that the web hosting provided by my University
does not provide mySQL as standard (most of my current sites are university
related and it makes sense to keep them on Uni hosting) but I have actually
found that using flat files instead of a database can be /more/ productive
than a database for many applications. The reason for this is simple - PHP
object serialization. I have two very simple functions:
serializeToFile($object, $filename) and unserializeFromFile($filename) -
these allow me to quickly and easily store any PHP object (or array or
other data structure) in a file somewhere. For small sites I have found
this is all I need for persistant data storage and the fact that I am
manipulating objects directly makes for much simpler code in administration
sections and makes displaying information a breeze.

One site I built recently ( www.bath.ac.uk/busu/ents/ ) uses only two lines
to display a page (each page is stored as a serialized object):

$pageObject = unserializeFromFile($requestedPageName'.dat');
$pageObject->display();

I find databases essential for complex or high traffic sites, but these
days I find flat files (with serialized objects) are perfectly suited to
smaller projects.

Regards,

Simon Willison




More information about the thelist mailing list