Tag Archives: featured

CouchDb, Creating views

The first couchDb tutorial that i’ve wrote was about simple operations like creating, updating or deleting documents, in this post i will explain how to create views, and how to call them ;

Creating views

The simplest way to create views is to use “Futon” (the phpmyadmin like for couchDb) accessible via :

http://localhost:5984/_utils/

And if everything is ok we should have something like this :

Then choose database where to create View, then on the select box in front of ‘View‘ select ‘Temporary view‘ :

When you click run button :

the last time we have created database called ‘contacts‘ with documents having this structure :

{
   "_id": "johnsmith",
   "_rev": "3-94052866ba060f57508fde265a243b47",
   "firstName": "John",
   "lastName": "Smith",
   "email": [
       "johnsmith@example.com"
   ],
   "phone": "(555) 555-5555"
}

The default function to show database content is :

function(doc) {
  emit(null, doc);
}

And when you hit run it will display all entries from the database “contact“, know let’s try to get contacts with a specific criteria, for example : “all contacts having a phone number” :

function(doc) {
  if(doc.phone){
	emit(doc._id, doc.phone);
  }
}

As result :

Saving the temporary view

Okey now that we are satisfied of the result let’s save the temporary view a permanent one :

  1. Click “Save As” button
  2. Set a design Name
  3. Set a view Name

That’s all ! our view will be accessible from the “View” Select Box :

Getting view result with curl

To query the server will use curl :

curl -X GET http://127.0.0.1:5984/contacts/_design/contacts/_view/phones

Result :

{"total_rows":1,"offset":0,"rows":[
  {"id":"johnsmith","key":"johnsmith","value":"(555) 555-5555"}
]}

Some explanations about params passed to curl :
– First argument “contact” is database name
– Second argument “_design” with value “contacts” is the design name that we have set when we saved the view
– Third argument “_view” with value “phone” is simply the view name

We can also pass other params at the end to tell the server how many entries to return, or to order them :

curl -X GET http://127.0.0.1:5984/contacts/_design/contacts/_view/phones?limit=11&descending=true

Hope this was helpful to understand how to create simple views with couchDb.

WSOD, White screen of death how to deal with it

Sometimes it happens that website  turns into a “beautiful” white page and nothing in the source code of the HTML generated code can help to find why.

This article is meant to help developers to find the common reasons.

“Invisible” Errors :

If error reporting is turned off fatal errors are not showed up (on production environment for example) and this can be the origin of white page. So it’s important to enable it on developement / testing environments, and you can do this by :

Enabling error reporting :

  error_reporting(E_ALL);
  ini_set('display_errors', TRUE);
  ini_set('display_startup_errors', TRUE);

Using .htaccess file :

  # PHP error handling for development servers
  php_flag display_startup_errors on
  php_flag display_errors on
  php_flag html_errors on
  php_flag log_errors on

Here you can find a nice article about .htaccess directives to handle errors.

Apache logs :

Also another way to track errors is to look on apache logs. The location of these files depends on what you have configured / apache version / vhosts configs .. etc.

To find apache errors logs phpinfo() can be helpfull, look for variable called : error_log

Or by default (on linux) apache logs are on /var/log/apache2/ directory.

To find errors :

  grep -r 'ErrorLog' /var/log/apache2/*

Whitespace at the end of PHP scripts :

This is the common error code that gives WSOD, so one of the best PHP coding practices is to avoid closing php tag (?>) at the end of script.

Infinite loops :

Infinite loops can cause WSOD, but apache can detect this on it will report it on error logs with something like  this :

[notice] child pid ##### exit signal Illegal instruction (4)

To sum up it’s important to know where to search, so first of all error reporting must be active to help to find the origin of the problem. On production environments debug can be tricky so you need to have apache logs accessible.

Apache2, Simply create vhosts

www.rabbit.com is the new vhost that we are trying to create, first we have to copy the default vhost configuration :

sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/www.rabbit.com

It’s important to know that apache2 loads configurations from /etc/apache2/sites-enabled/ so we have to add a link of our vhost configuration on this directory, then we have to change the configuration  :

<VirtualHost *:80>
        ServerAdmin admin@rabbit.com
        ServerName www.rabbit.com
        DocumentRoot /home/www.rabbit.com
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /home/www.rabbit.com>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog /var/log/apache2/rabbitcom.error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /var/log/apache2/rabbitcom.access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

It’s important to add the directive :


ServerName www.rabbit.com

And to change the location of your directory.

For the activation of the vhost we can use :

sudo a2ensite www.rabbit.com

Or create a link :

ln -s /etc/apache2/sites-available/www.rabbit.com /etc/apache2/sites-enabled/www.rabbit.com

Drupal 6, avoiding message ‘An illegal choice has been detected’.

Drupal form control can be tiresome some times, especially when developping custom modules .. In my case it was the integration of country and region select boxes from the ubercart module.

In my implementation of hook_form_alter we are adding these two elements in code :

$select_country = uc_country_select(uc_get_field_name('country'), $country, NULL, 'name');
$select_zone = uc_zone_select(uc_get_field_name('zone'), $zone, NULL, $country, 'name');

$form['country'] = $select_country;
$form['zone'] = $select_zone;

But when submitting the form we have always the same error : An illegal choice has been detected ??!!

As a solution and reading validation from code it’s possible to add in the form alter the information that this form has been already validated :

$form['zone']['#validated'] = true;

MySQL, Database table history using Schemas and triggers

Assuming that we have to trace every intervention on a database called « stats»

As result we wil have a new database with the “history_” prefix which will contain all the same tables of our source database “stats” but with different column definition. We have to add 3 new colums.

  1. Operation.
  2. Time.
  3. Operation made.
  4. Creating the histroy database.

To continue we need to be sure that the mysql user has all privilieges for creating databases and Triggers.

Continue reading MySQL, Database table history using Schemas and triggers