Programming Economic Experiments with Php/mysql

with contributions by:
Jeroen van de Ven
Jona Linde
Roel van Veldhuizen
Lucas Molleman
Joep Sonnemans
Boris van Leeuwen

Why php/mysql?

Very likely you spend quite a lot of time each day looking at webpages: reading news, checking email, social networks, shopping, etc., on your computer, tablet and smartphone. If you visit a webpage of 10 years ago you will realize how much has changed. Movies, sound, animations are common now and clicking with a mouse is not the only way of interaction anymore; you can chat with others, and webpages can even use your webcam and microphone. Is there anything you cannot do within a webpage? If you use a package (like ztree) to program economic experiments, you will always be limited in your possibilities. Not if you use webpages!
Php is a computer language that produces web (html) pages. It is open source: free and you don't have to sign anything (it is not a temporary license as some free packages have). It is used by millions of people and there are many webpages that can help you program. Whenever I have a problem (most of the time because I forgot how to do something) I have a solution within 3 clicks in google. There are also many code-snippets around.
Mysql is an open source database program. We use it to save the parameters in an experiment, and to save the decisions of the participants during the session.
Javascript is a language that is run on the client computer (in contrast to php and mysql that runs on the server). It started as a very basic-like language, but has evolved into a full (object-based) language. To program an experiment you don't need much knowledge of javascript. The most javascript I have used I have copied form internet and adapted to my own use.

What you need (to know)

You need a webserver with php and mysql. It is easiest to download and install a combined (free): XAMPP package (windows, OSX and linux). If you use a mac an alternative for XAMPP is the MAMP package (I advise against using the default Apache installation of OSX). The packages includes phpmyadmin which is a very nice interface with mysql (of course in a webpage and of course using php).
You have to learn the basics html and php (for example on I suggest you first play an afternoon or so with html before you continue to php.


We have at least two computers: the client (the computer of the subject) is running a browser like firefox or chrome, and the server computer is running a web-server (apache) with php and mysql. Mysql is a database program and php is a language that produces html-files that are readable by browsers. The client asks for a page (1), say "instructions.php". The server gets this file and interprets it. Probably the page asks to get some data from a specific database (2), for example the payoff structure, and sometimes some data is written to the database (for example when the subject has made a decision). The server sends back html-data to the client computer (3) and the browser takes care that this is displayed on the screen of the subject.


There are two ways to read/write data from mysql database:
1. Php-pages during experiment. Because many queries are quite standard, we have included some functions in the for reading and writing data. For example $numberplayers=readcommonparameter("numberplayers"); opens the table commonparameters in the database, looks for the record with name numberplayers and returns the value of that parameter. Or the function insertRecord("decisions","ppnr, period, decision","\"$ppnr\", \"$period\", \"$decision\""); inserts a new record, in this example in the table decisions a record where the fields ppnr, period and decision are filled with the values of the php variables $ppnr, $period, $decision, respectively.
2. Phpmyadmin: before (setup database) and after experiment (export data in sql or excel format).

Learning by example

If you know the basics of html and php, and have XAMPP or MAP installed, you now may want to go through an example project: Prisoner's dilemma .