Working with PHP's DBA Functions
See all Sams Teach Yourself on InformIT Web Development Tutorials.
If you don't have access to a SQL database such as MySQL or Oracle, you will almost certainly have a DBM-style database system available to you. DBM stands for database manager and DBM-like systems allow you to store and manipulate name/value pairs on your system.
DBA stands for Database abstraction layer, and these functions are designed to provide a common interface to a range of file-based database systems.
Although DBA functions do not offer you the power of a SQL database, they are flexible and easy to use. The fact that DBA functions stand above a range of common database systems, means that your code is likely to be portable even if the database files themselves might not be.
In this hour, you will learn:
How to open a database
How to add data to the database
How to extract data from the database
How to change and delete items
How to store more complex kinds of data in DBM-style databases
Beneath the Abstraction
In order to use the DBA functions, you need to have one of the supported database systems installed. If you are running Linux, it is likely that you will have GDBM (the GNU Database Manager installed). For each system there is a corresponding compile option which should have been used when PHP was installed. You can see the supported databases and their corresponding compile options in Table 11.1.
Table 11.1 DBM systems supported by the DBA functions
Type |
Compile option |
Further information |
cdbm |
--with-cdbm |
Read-only database system |
db2 |
--with-db2 |
|
db3 |
--with-db3 |
|
dbm |
--with-dbm |
The original DBM. Deprecated |
gdbm |
--with-gdbm |
GNU Database Manager |
ndbm |
--with-ndbm |
Deprecated |
If your system and PHP installation supports one of these systems you will be able to use the DBA functions with no problems. Note that support for the cdbm system (which is designed for fast access to static databases) is read-only.