You have data that can be easily represented as key/value pairs, want to store it safely, and have very fast lookups based on those keys.
Use the DBA abstraction layer to access a DBM-style database, as shown in Example 10-3.
Using a DBM database
PHP can support a few different kinds of DBM backends: GDBM, NDBM, DB2 , DB3, DBM, and CDB. The DBA abstraction layer lets you use the same functions on any DBM backend. All these backends store key/value pairs. You can iterate through all the keys in a database, retrieve the value associated with a particular key, and find if a particular key exists. Both the keys and the values are strings.
The program in Example 10-4 maintains a list of usernames and passwords in a DBM database. The username is the first command-line argument, and the password is the second argument. If the given username already exists in the database, the password is changed to the given password; otherwise, the user and password combination are added to the database.
Tracking users and passwords with a DBM database
The dba_open( ) function returns a handle to a DBM file (or false on error). It takes three arguments. The first is the filename of the DBM file. The second argument is the mode for opening the file. A mode of r opens an existing database for read-only access, and w opens an existing database for read-write access. The c mode opens a database for read-write access and creates the database if it doesn't already exist. Last, n does the same thing as c, but if the database already exists, n empties it. The third argument to dba_open( ) is which DBM handler to use; this example uses 'gdbm'. To find what DBM handlers are compiled into your PHP installation, look at the "DBA" section of the output from phpinfo( ). The "Supported handlers" line gives you your choices.
To find if a key has been set in a DBM database, use dba_exists( ). It takes two arguments: a string key and a DBM filehandle. It looks for the key in the DBM file and returns true if it finds the key (or false if it doesn't). The dba_replace( ) function takes three arguments: a string key, a string value, and a DBM filehandle. It puts the key/value pair into the DBM file. If an entry already exists with the given key, it overwrites that entry with the new value.
To close a database, call dba_close( ) . A DBM file opened with dba_open( ) is automatically closed at the end of a request, but you need to call dba_close( ) explicitly to close persistent connections created with dba_open( ).
You can use dba_firstkey( ) and dba_nextkey( ) to iterate through all the keys in a DBM file and dba_fetch( ) to retrieve the values associated with each key. The program in Example 10-5 calculates the total length of all passwords in a DBM file.
Calculating password length with DBM
The dba_firstkey( ) function initializes $k to the first key in the DBM file. Each time through the while loop, dba_fetch( ) retrieves the value associated with key $k and $total_length is incremented by the length of the value (calculated with strlen( )). With dba_nextkey( ), $k is set to the next key in the file.
One way to store complex data in a DBM database is with serialize( ). Example 10-6 stores structured user information in a DBM database by serializing the structure before storing it and unserializing when retrieving it.
Storing structured data in a DBM database
While Example 10-6 can store multiple users' data in the same file, you can't search for, for example, a user's last access time, without looping through each key in the file. If you need to do those kinds of searches, put your data in an SQL database.
Each DBM handler has different behavior in some areas. For example, GDBM provides internal locking. If one process has opened a GDBM file in read-write mode, other calls to dba_open( ) to open the same file in read-write mode will fail. For other DBM handlers, add an l to the mode you pass to dba_open( ) to lock the database with a separate .lck file or a d to lock the database file itself. Two DBA functions are also database-specific: dba_optimize( ) and dba_sync( ). The dba_optimize( ) function calls a handler-specific DBM file-optimization function. Currently, this is implemented only for GDBM, for which its gdbm_reorganize( ) function is called. The dba_sync( ) function calls a handler-specific DBM file synchronizing function. For DB2 and DB3, their sync( ) function is called. For GDBM, its gdbm_sync( ) function is called. Nothing happens for other DBM handlers.
Using a DBM database is a step up from a plain text file but it lacks most features of an SQL database. Your data structure is limited to key/value pairs, and locking robustness varies greatly depending on the DBM handler. Still, DBM handlers can be a good choice for heavily accessed read-only data.
10.1.4. See Also
Recipe 5.7 discusses serializing data; documentation on the DBA functions at http://www.php.net/dba; for more information on the DB2 and DB3 DBM handlers, see http://www.sleepycat.com/products/bdb.html (note that these handlers are not generally free for commercial use); for GDBM, check out http://www.gnu.org/directory/gdbm.html or http://www.mit.edu:8001/afs/athena.mit.edu/project/gnu/doc/html/gdbm_toc.html .