DBM Functions Demo

Functions demonstrated
<?dbmopen($filename)>
<?dbmclose($filename)>
<?dbminsert($filename,$key,$content)>
<?dbmreplace($filename,$key,$content)>
<?dbmfetch($filename,$key)>
<?dbmdelete($filename,$key)>
<?dbmfirstkey($filename)>
<?dbmnextkey($filename,$key)>

These functions should look very familiar to anybody who has worked with dbm files from C before. For those of you that haven't, dbm files are hashed files containing key/content pairs of data. They allow you to quickly look up keyed data stored on disk.

The unique aspect of the dbm support in PHP is that it works with ndbm, GNU's gdbm, Berkeley's libdb or even no dbm support in which case an internal flat ascii file format is used. GNU's gdbm library available from prep.ai.mit.edu is probably your best choice if you don't have built-in ndbm support in your operating system.

dbmopen() opens a dbm file. The argument is the full-path filename of the dbm file to be opened. Note that if ndbm support is used, ndbm will actually create $filename.dir and $filename.pag files. gdbm only uses one file, as does the internal flat ascii file support. Note that PHP does its own file locking in addition to any file locking this may be done by the dbm library itself.

dbmclose() simply closes the specified dbm file. It will also remove any lock files, so it is important to close any dbm files that have been opened.

dbminsert() inserts a new key/content data pair into a dbm file. If the key already exists, the insert will fail.

dbmreplace() is similar to the dbminsert() function, the only difference being that if the key already exists, the old content string will be replaced with the new.

dbmfetch() will return the content string associated with the given key.

dbmdelete() will delete the key/content pair specified by the given key argument.

dbmfirstkey() returns the first key in the dbm file. Note that no particular order is guaranteed since the order depends on hash table values calculated within the dbm implementation. You may use the sort function to sort arrays of data from the dbm file if necessary.

dbmnextkey() returns the next key after the specified key. By calling dbmfirstkey() followed by successive calls to dbmnextkey() it is possible to visit every key/content pair in the dbm file.

See the dbm man pages on your Unix system for more information on dbm files.