# DBM-style Database Abstraction PHP

The DBA functions abstract communications with databases that conform to the style of Berkeley DB database systems. Rather than storing complex records, a DBM database simply stores key/value pairs. This is similar to an associative array. The functions in this section replace a set of functions that allow just one type of DBM database. These new functions allow for choosing the underlying system from within your PHP code rather than compiling PHP for a single DBM implementation. You choose a type of database when you open a connection, and the rest of the functions perform accordingly. Sascha Schumann added these functions to PHP.

The dba_close function closes a link to a database. The link argument is an integer returned by the dba_open or dba_popen functions. If you choose not to close a database connection, PHP will close it for you.

The dba_delete function removes an entry from a database. You must supply both the key and a valid link to a database, as supplied by dba_open or dba_popen. The success of the delete is returned as a boolean.

<?
// open database in write mode
$db = dba_popen('inventory', 'w', 'gdbm'); if($db)
{
//check for record
if(dba_exists('3', $db)) { // remove item 3 dba_delete('3',$db);
}
else
{
print('Record does not exist');
}
// close database
dba_close($db); } else { print('Database does not exist'); } ?> boolean dba_exists(string key, integer link) The dba_exists function tests for the presence of a key. The link argument must be an integer returned by the dba_open or dba_popen functions. The description of dba_delete has an example of using dba_exists. string dba_fetch(string key, integer link) Use the dba_fetch function to retrieve a record. <? // open database in write mode$db = dba_popen('inventory', 'r', 'gdbm');
if($db) { //loop over each record for($key = dba_firstkey($db);$key; $key=dba_nextkey($db))
{
print("$key = "); //fetch this record print(dba_fetch($key, $db)); print("<BR>n"); } } else { print('Database does not exist'); } ?> string dba_firstkey(integer link) The dba_firstkey function returns the first key in the database. If the database is empty, FALSE will be returned. As the example for dba_fetch shows, dba_firstkey and dba_nextkey may be used to traverse the entire database. boolean dba_insert(string key, string value, integer link) Use dba_insert to add a record to the database. The success of the insert is returned. Trying to insert a record that already exists is not allowed. If you need to update a record, use dba_replace. <? // open database in write mode$db = dba_popen('inventory', 'w', 'gdbm');
if($db) { //check for record if(dba_exists('3',$db))
{
//item 3 exists, set inventory to 150
dba_replace('3', '150', $db); } else { //item 3 doesn't exists, insert it dba_insert('3', '150',$db);
}
// close database
dba_close($db); } else { print('Database does not exist'); } ?> string dba_nextkey(integer link) The dba_nextkey function returns the next key from the database. When there are no keys left, false is returned. The description of dba_fetch shows a typical use of dba_nextkey and dba_firstkey together. integer dba_open(string filename, string mode, string type, ...) Use dba_open to establish a link to a dbm-style database. A positive integer will be returned if the open is successful, FALSE if it fails. The filename argument is simply the path to a database. The mode argument can be one of four characters that control input and output of data. The type argument chooses the underlying database engine. You may also supply any number of optional arguments that will be passed directly to the underlying engine. When your script finishes executing, the database link will close automatically. You may choose to close it sooner with dba_close, and this may save some small amount of memory. Contrast this function to dba_popen, which attempts to reuse links. boolean dba_optimize(integer link) Use dba_optimize to optimize a database, which usually consists of eliminating gaps between records created by deletes. This function returns TRUE on success. Some underlying engines do not support optimizations, in which case this function will have no effect. <? // open database in write mode$db = dba_popen('inventory', 'w', 'gdbm');
if($db) { //optimize database dba_optimize($db);
// close database
dba_close($db); } else { print('Database does not exist'); } ?> integer dba_popen(string filename, string mode, string type, ...) The dba_popen function behaves identically to dba_open with one difference: Links are not closed. They remain with the process until the process ends. When you call dba_popen, it first tries to find an existing link. Failing that, it will create a new link. You never call dba_close on a link returned by dba_popen. Since the links are pooled on per-process basis, this functionality offers no benefit when using PHP as a stand-alone executable. When used as an Apache module, there may be some small performance benefit due to the unique way Apache uses child processes. boolean dba_replace(string key, string value, integer link) Use dba_replace to update the value of an existing record. As with the other DBA functions, a valid link as returned by dba_open or dba_popen should be used for the link argument. See the description of dba_insert for an example using dba_replace. boolean dba_sync(integer link) The dba_sync function will synchronize the view of the database in memory and its image on the disk. As you insert records, they may be cached in memory by the underlying engine. Other processes reading from the database will not see these new records until synchronization. <? // open database in write mode$db = dba_popen('inventory', 'w', 'gdbm');
if($db) { for($n=1; $n=10;$n++)
{
//insert row
dba_insert($n, '',$db);
//synchronize
dba_sync($db); } // close database dba_close($db);
}
else
{
print('Database does not exist');
}
?>

0