<?php // Create a new Db4 Instance $db = new Db4(); // Open it outside a Db4Env environment with datafile db4 // and database name "test." This creates a non-transactional database $db->open(null, "./db4", "test"); // Get the current value of "counter" $counter = $db->get("counter"); print "Counter Value is {$counter}\n"; // Increment $counter and put() it. $db->put("counter", $counter + 1); // Sync to be certain, since we're leaving the handle open $db->sync();
<?php // Open a new Db4Env $dbenv = new Db4Env(); $dbenv->set_data_dir("/var/tmp/dbhome"); $dbenv->open("/var/tmp/dbhome"); // Open a database in $dbenv. Note that even though // we pass null in as the transaction, db4 forces this // operation to be transactionally protected, so PHP // will force auto-commit internally. $db = new Db4($dbenv); $db->open(null, 'a', 'foo'); $counter = $db->get("counter"); // Create a new transaction $txn = $dbenv->txn_begin(); if ($txn == false) { print "txn_begin failed"; exit; } print "Current value of counter is {$counter}\n"; // Increment and reset counter, protect it with $txn $db->put("counter", $counter + 1, $txn); // Commit the transaction, otherwise the above put() will rollback. $txn->commit(); // Sync for good measure $db->sync(); // This isn't a real close, use _close() for that. $db->close();
<?php // Create a new Db4 Instance $db = new Db4(); // Open it outside a Db4Env environment with datafile /var/lib/db4 // and database name "test." This creates a non-transactional database $db->open(null, "/var/tmp/db4", "test"); // Get the current value of "counter" $counter = $db->get("counter"); print "Counter Value is {$counter}\n"; // Increment $counter and put() it. $db->put("counter", $counter + 1); // Sync to be certain, since we're leaving the handle open $db->sync();
<?php // Open a new Db4Env. By default it is transactional. The directory // path in the open() call must exist. $dbenv = new Db4Env(); $dbenv->set_data_dir("/var/tmp/dbhome"); $dbenv->open("/var/tmp/dbhome"); // Open a database in $dbenv. $db = new Db4($dbenv); $txn = $dbenv->txn_begin(); $db->open($txn, 'a', 'foo'); $txn->commit(); $counter = $db->get("counter"); // Create a new transaction $txn = $dbenv->txn_begin(); if ($txn == false) { print "txn_begin failed"; exit; } print "Current value of counter is {$counter}\n"; // Increment and reset counter, protect it with $txn $db->put("counter", $counter + 1, $txn); // Commit the transaction, otherwise the above put() will rollback. $txn->commit(); // Sync for good measure $db->sync(); // This isn't a real close, use _close() for that. $db->close();
session_start(); if (empty($_SESSION['count'])) { $_SESSION['count'] = 1; } else { $_SESSION['count']++; } $request_method = $_ENV['REQUEST_METHOD']; $request_uri = substr($_ENV['REQUEST_URI'], 1); $content_length = $_ENV['CONTENT_LENGTH']; $filename = "database"; $db = new Db4(); if ($request_method === 'GET') { if (!empty($_SESSION[$request_uri])) { echo $_SESSION[$request_uri]; } else { $db->open(NULL, $filename, NULL); $val = $db->get($request_uri); echo $val; } } else { if ($request_method === 'PUT') { $buffer = file_get_contents('php://input'); $_SESSION[$request_uri] = $buffer; } else { if ($request_method === 'DELETE') { unset($_SESSION[$request_uri]); $db->open(NULL, $filename, NULL); $db->del($request_uri); } } }