One of the most powerful aspects of PHP is the ease with which a programmer can connect to a database and manipulate its contents. With just a few lines of code the developer can run a SQL query on the database and return the information to a the PHP application user.
However, a database may not always be available. For example the cost of web hosting may increase if a database (such as MySQL) is included in a package. In this case the PHP programmer will need to look at an alternative way of storing information, and one such solution in the PHP Text Database API.
The PHP Text Database API allows the developer to use text files just as if they were tables in a database, and the programmer can run SQL queries on these tables just like any other database. There are some limitations (such as only three data types) but the PHP Text Database API can be a quick and easy answer for any developers that do not have access to the more commonly used databases.
Installing the PHP Text Database API
The PHP Text Database API (or PHP-TextDB API) can be downloaded from http://www.c-worker.ch/txtdbapi/index_eng.php and is stored in a zip file. When the file is decompressed it will create a new directory (something like php-txt-db-api-0.3.1-Beta-01).
This folder contains all of the files required for the PHP Text Database API, including an extensive help file. It can be renamed if required (for example to php-txt-db-api) and can be moved anywhere that is accessible to PHP - it does not have to be in the htdocs area of the web server.
Once the files have been downloaded then configuration is very simple.
Configuring the PHP Text Database API
The PHP Text Database API folder contains an important file named txt-db-api.php. This is the API's master include file and it contains two vital variables that must be set:
- $API_HOME_DIR - this must be set to the directory that contains the PHP Text Database API files
- $DB_DIR - this is the folder that will contain any databases that are created
For example these may look something like:
This means, of course, that the developer can either have one global txt-db-api.php or an individual one for each PHP application that they develop.
Creating a New Database
Before creating a new database it is, of course, necessary to include the txt-db-api.php file:
It is then a good idea to ensure that the database directory actually exists:
The database can then be created in one of two ways. The first is by using the SQL create database statement:
Or by using the mkdir statement:
The next stage is to create the tables for the database.
Creating a New Database Table
Each table is created by using the SQL create table statement, however it must be remembered that the PHP Text Database API only accepts 3 data types
- inc - an auto-incremental field
- int - an integer field
- str - a text field
For example:
With the table in place then its contents can be viewed and manipulated by using SQL statements.
Working with Database Tables
Although the files are just text files SQL can still be used on them, for example to insert data:
and also to display the contents:
And so, although the PHP Text Database API is not a replacement for a databases such as MySQL, it is a good substitute when they are not available.