One of the things that is being done with our project, Scrib, is building it to use CouchDB as the back-end database. The data we’ll be storing is more suited to a ‘document-style’ database rather than relational, like MySQL (here’s some pros and cons of CouchDB vs. MySQL). One of the great bonuses is that it uses a RESTful HTTP-API, which means that all of the data is accessible via HTTP requests. It also uses JSON for data transfer and storage. This makes it relatively easy to create a web-interface as the data is in a format that is not defined (like XML) and can easily be parsed.
While Scrib is utilizing it through Python, the web interface is going to be built using a mixture of HTML, JavaScript, CSS and PHP.
Utilizing PHP on Couch
For this part of the project we are going to use PHP on Couch, a PHP wrapper class to make things easy.
Most of the time CouchDB is already installed inside Linux (Debian-based distros, at least). In Ubuntu you can install it via “sudo apt-get install couchdb” (those of you who are using MacOSX or Windows, you can find respective packages here and here.)
Here’s an example of how easy it is to use CouchDB inside PHP:
<?php
require_once 'couch.php';
require_once 'couchClient.php';
require_once 'couchDocument.php';
// The default port for CouchDB is 5984. Let's connect to it!
$client = new couchClient('http://path.to.server:5984','db_name');
// A quick test to see if our connection works:
// Connect to our database:
$doc = new couchDocument($client);
// Creating a document and adding in the first entry
$doc->set(array('_id'=>'UristMcMiner','name'=>'McMiner','firstname'=>'Urist'));
// Some quick tests
echo $doc->firstname . " " . $doc->name ; // should echo "Urist McMiner"
echo "<br/>"; // This will make a new line.
echo $doc->_id; // This would echo "UristMcMiner"
// This would change the document property of "name" to "McFarmer"
// and store the updated document in the database.
$doc->name = "McFarmer";
echo $doc->firstname . " " . $doc->name ; // should echo "Urist McFarmer"
?>