Using Berkeley DB with PHP

A PHP 4 extension for this release of Berkeley DB is included in the distribution package. It can either either link directly against the installed Berkeley DB library (which is necessary for running in a non-Apache/mod_php4 environment), or against mod_db4, which provides additional safety when running under Apache/mod_php4.

For installation instructions, see the INSTALL file that resides in the lang/php_db4 directory in your Berkeley DB distribution.

The PHP extension provides the following classes, which mirror the standard Berkeley DB C++ API.

  1. class Db4Env {
  2. function Db4Env($flags = 0) {}
  3. function close($flags = 0) {}
  4. function dbremove($txn, $filename, $database = null, $flags = 0) {}
  5. function dbrename($txn, $file, $database, $new_database,
  6. $flags = 0) {}
  7. function open($home, $flags = DB_CREATE | DB_INIT_LOCK |
  8. DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN,
  9. $mode = 0666) {}
  10. function remove($home, $flags = 0) {}
  11. function add_data_dir($directory) {}
  12. function txn_begin($parent_txn = null, $flags = 0) {}
  13. function txn_checkpoint($kbytes, $minutes, $flags = 0) {}
  14. }
  15. class Db4 {
  16. function Db4($dbenv = null) {} // create a new Db4 object using
  17. // the optional DbEnv
  18. function open($txn = null, $file = null, $database = null,
  19. $flags = DB_CREATE, $mode = 0) {}
  20. function close() {}
  21. function del($key, $txn = null) {}
  22. function get($key, $txn = null, $flags = 0) {}
  23. function pget($key, &$pkey, $txn = null, $flags = 0) {}
  24. function get_type() {} // returns the stringified
  25. // database type name
  26. function stat($txn = null, $flags = 0) {} // returns statistics as
  27. // an as
  28. function join($cursor_list, $flags = 0) {}
  29. function sync() {}
  30. function truncate($txn = null, $flags = 0) {}
  31. function cursor($txn = null, flags = 0) {}
  32. }
  33. class Db4Txn {
  34. function abort() {}
  35. function commit() {}
  36. function discard() {}
  37. function id() {}
  38. function set_timeout($timeout, $flags = 0) {}
  39. }
  40. class Db4Cursor {
  41. function close() {}
  42. function count() {}
  43. function del() {}
  44. function dup($flags = 0) {}
  45. function get($key, $flags = 0) {}
  46. function pget($key, &$primary_key, $flags = 0) {}
  47. function put($key, $data, $flags = 0) {}
  48. }

The PHP extension attempts to be “smart” for you by:

  1. Auto-committing operations on transactional databases if no explicit Db4Txn object is specified.
  2. Performing reference and dependency checking to insure that all resources are closed in the correct order.
  3. Supplying default values for flags.