Getting started with CouchDB and PHP 5 on Ubuntu 10.10

Post to Twitter

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
$ ./chmod +x

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:
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:

set( array('type'=>'gadget','name'=>'iPad2','manufacturer'=>'Apple') );  
  echo 'Entered: ' . $doc1->name . '
'; $doc2->set( array('type'=>'gadget','name'=>'Xoom','manufacturer'=>'Motorola') ); echo 'Entered: ' . $doc2->name . '
'; ?>

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:

Let’s write a query to get all the documents back:

  echo "Database has " . $all_docs->total_rows . " documents

"; foreach ( $all_docs->rows as $row ) { echo "Document ID: " . $row->id . "
"; $doc = $client->getDoc($row->id); echo "Name: " . $doc->name . "
"; echo "Manufacturer: " . $doc->manufacturer . "

"; } ?>

Updating a document is pretty easy:

$doc = $client->getDoc("abf459c069a30767d59d3b33ab0027bd");
$doc->name = "iPad3";

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");

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).

Post to Twitter

This entry was posted in CouchDB, Open Source, PHP, Ubuntu. Bookmark the permalink.

2 Responses to Getting started with CouchDB and PHP 5 on Ubuntu 10.10

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

  2. Anand George says:

    Fantastic post. Flawless.

Comments are closed.