Creating a File-Based DatabaseBefore actually using the dba functions, you have to create a database file. This is done using the function dba_open(). Similar to PHP's fopen(), this function expects both a filename and a file mode. The third parameter is the name of the handler used. Listing 26.2 (dba_create.php) creates a file called dba.db and uses the handler provided in handler.inc.php. Upon success, the database is closed using dba_close(). Listing 26.2. The Include File for the dba Handler Used<?php require_once "handler.inc.php"; $dba = @dba_open("dba.db", "n", $dbahandler); if (!$dba) { echo "Failed creating database."; } else { echo "Succeeded creating database."; dba_close($dba); } ?>
For the file mode, the following options are available:
Only the modes c and n support creation of the database file. Also, there are some options for file locking:
On the manual page for PHP's dba functions, http://php.net/dba, you will find an overview table that shows which locking modes let which write operations fail. Because mode c is not supported by all dba handlers, Listing 26.2 uses mode n to create the database. This is usually a step that is done only once. Afterward, you will most probably use either read or write access and stick to the (more performance and reliable) modes r and w.
![]() |