CouchDB according to Wikipedia “is an open source document-oriented database written in the Erlang programming language. It borrows from NoSQL and is designed for local replication and to scale horizontally across a wide range of devices”. CouchDB is an Apache project. Today I’ll use Ubuntu 10.10 and PHP 5.3+ to talk to CouchDB using the PHP-on-Couch library.
Assuming you have a clean install of Ubuntu 10.10 running here are the steps I did to get everything up and running:
$ sudo apt-get update $ sudo apt-get install apache2 $ sudo apt-get install php5 php5-dev libapache2-mod-php5 php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ps php5-pspell php5-recode php5-snmp php5-tidy php5-xmlrpc php5-xsl php5-common $ sudo /etc/init.d/apache2 restart
If you want to install CouchDB via apt-get then all you need to do is this:
$ sudo apt-get install couchdb
You can also install CouchDB from source if you want a more current version.
Finally, you can go to CouchOne and grab the Ubuntu all inclusive installer and all you need to do then is:
$ chmod +x install-couchdb-1.0.2_rel1-linux.run $ ./chmod +x install-couchdb-1.0.2_rel1-linux.run
Start the server (if its not already). If you have issues installing please consult the forums or do a Google search.
Once CouchDB is installed open a browser and go to this link: http://127.0.0.1:5984/_utils/
Futon should open up which is a front end to work with CouchDB. Create a new database called myphpdemo.
Go and download (or GIT clone) PHP-on-Couch from GitHub. Extract the lib folder and put it to wherever you are going to run your PHP project from. For simplicity I put the lib folder in /var/www/couchstuff. I then created a PHP file in the /var/www/couchstuff folder and called it couch.php. Your folder structure should look like this:
Here is the code for couch.php:
<?PHP
require_once 'lib/couch.php';
require_once 'lib/couchClient.php';
require_once 'lib/couchDocument.php';
$client = new couchClient ('http://localhost:5984','myphpdemo');
$doc1 = new couchDocument($client);
$doc2 = new couchDocument($client);
$doc1->set( array('type'=>'gadget','name'=>'iPad2','manufacturer'=>'Apple') );
echo 'Entered: ' . $doc1->name . '<br />';
$doc2->set( array('type'=>'gadget','name'=>'Xoom','manufacturer'=>'Motorola') );
echo 'Entered: ' . $doc2->name . '<br />';
?>
This code will make a connection to CouchDB and create two new documents.
You can go back into Futon to verify they actually got entered, use this URL: http://127.0.0.1:5984/_utils/database.html?myphpdemo
Let’s write a query to get all the documents back:
<?PHP
require_once 'lib/couch.php';
require_once 'lib/couchClient.php';
require_once 'lib/couchDocument.php';
$client = new couchClient ('http://localhost:5984','myphpdemo');
$all_docs = $client->getAllDocs();
echo "Database has " . $all_docs->total_rows . " documents<br /><br />";
foreach ( $all_docs->rows as $row )
{
echo "Document ID: " . $row->id . "<br />";
$doc = $client->getDoc($row->id);
echo "Name: " . $doc->name . "<br />";
echo "Manufacturer: " . $doc->manufacturer . "<br /><br />";
}
?>
Updating a document is pretty easy:
$doc = $client->getDoc("abf459c069a30767d59d3b33ab0027bd");
$doc->name = "iPad3";
$client->storeDoc($doc);
Using the Id that was displayed you can call the getDoc and then make your update and call storeDoc.
Deleting a document is just as easy:
$doc = $client->getDoc("abf459c069a30767d59d3b33ab0027bd");
$client->deleteDoc($doc);
Please keep in mind I have only shown a little of what CouchDB and the PHP-on-Couch library can do. Check out the documentation for CouchDB and PHP-on-Couch for more in-depth details. PHP-on-Couch also has some easy to follow example code (with error handling which I left out of this article for brevity).










hi, thank you very much. but how con we insert php variables ($whatever) insted of static strings???
Fantastic post. Flawless.