Build Your Own Database 1

Build a Simple Flat File Database

Definiation of Database: "A structured set of data held in a computer, especially one that is accessible in various ways". (Lexico)

There must be infinite ways to organize, access and present data. Because I work with website applications, I chose to use text files as my database and PHP code to access and present the text as HTML. The following example illustrates the use of a text-file database with only a single table. In later examples, this will be expanded to include relational elements.

My database is composed of text documents that reside in the 'data' folder in my application. Database: 'data folder'; Tables: sub-folders; Records: text documents inside sub-folders.


 text file database folders
The'database-functions.php' file transforms these folders into a datase.





Files and Folders for this demo:



top level file structure for SMALL1 application

Contents of the 'data' folder
data folder for the small2 demo website


Contents of the 'stories' sub-folder


Contents of the 'pages' folder:


Contents of the 'admin-pages' folder


Contents of the 'inc' folder:


'index.php' file


Accessing the Database

The database functions convert text strings to associative arrays and back to strings to accomplish reading and writing operations. As an associative array, the text string can be used as a database record. The name of each text file in the database ends in '.txt'. By removing the '.txt', this name can be used as a record key by the database functions.