Dead Simple Flatfile Database

A Post by Jake, posted under Learning.

 
Dead Simple Flatfile Database

During the past couple of weeks I've been trying my hand at building a class in PHP that works as a simple flat file database (FFD). After looking around at the many project out there already taking advantage of FFD's with functionality and extra's that would take an extremely long time to match, I decided to give it a crack, even if it was dead simple.

I started building my file structure, because the database would use a file structure, I wanted to work out how I was going to set out the tables, this meant looking into other formats such as JSON and XML which are more commonly used. Just to add a bit of creativity into the project, I decided to see if I could create my own structure.. I call it PFDB (PHP Flat Database), I know brilliant right?

So I began by differentiating the table structure to the data as well as making it somewhat easy for the PHP Class to pick this up without getting confused, so I decided to use @tables. I created the file user.pfdb and added the following data:

@tables {id,forename,surname,email,password}
[1,"john","smith","[email protected]", "plaintext"];
[2,"steve","jobs","[email protected]", "password"];
[3,"alan", "jackson", "[email protected]", "hello1"];

Please note, never store your passwords in plaintext, for the sake of this post, I used plaintext to illustrate data in the records.

You will notice here I have three rows of records, each represent a user in my database - the next step was to save this and start creating the class in PHP; In order to keep this post short, I will explain how I finished the class rather than go through each line of code.

When I began building the PDFB class, I firstly checked to see if the file exists, if it didn't a function would be called which echoed an error to explaining the file wasn't found. After loading the data into a variable, using substr and strpos, I pulled the information between the first '@' and '}' in the variable, this would be the line @tables {id,forename,surname,email,password}

I then did some very messy str_replace and remove the original line from the variable so it wouldn't confuse the class and I had a very neat array containing the table rows. I then followed the same process as above with the records, this time running a for loop (more than one record), collecting the records and then used array_combine to combine my table rows to the records creating a very nice associative array, which I called $this->records.

With my newly made associative array I could print using $this->records['forename'] or any of the table rows I had in my users database. I then created a function named find which I used to do the job of SELECT in MySQL, this would retrieve either a single result or an array and would follow a similar query to that of SELECT.

After building the find function, I was able to print out data from my database either using a table row such as "forename" or all information of a record using "*" (all), below is a couple of examples of what find does:

Query: $db->find("* where forename = john");

Result: Array ( [id] => 1 [forename] => john [surname] => smith [email] => [email protected] [password] => plaintext )



Query: $db->find("forename where surname = jobs");

Result: steve



Query: $db->find("* where id = 3");

Result: Array ( [id] => 3 [forename] => alan [surname] => jackson [email] => [email protected] [password] => hello1 )

As you can see, using the find function I am able to do very simple queries with my flat file database, after testing with different queries/records against the clock, I found I could return information in around 0.59 seconds, which is fast!


View on Github

 

Copyright © 2015 Jake Bown. All Rights Reserved.