The Business Rules Engine

PHP: CodaClient for PHP

PHP is the first application language with a driver for CodaServer. This driver can be obtained right here.

Once you obtain the CodaClient for PHP, unzip the file. Place the codaserver.php file and HessianPHP directory in an appropriate place within your code. Simply require_once() codaserver.php and you should be good to go!

The other files in the distribution are tester.php, which lets you try out your CodaServer installation and run queries and commands against it. It can also be useful in a pinch when you don't have access to the full CodaClient. The documentation for the driver is under the docs directory. This documentation is also available here.

Some effort has been made to make the CodaClient for PHP consistent with other database drivers for PHP. That said, it has some different conventions and requirements that should be followed.


To connect to CodaServer, use the following code snippet:

$connection = codaserver_connect('hostname', 'port', 'username', 'password');

with each of the strings replaced by their appropriate values for your environment. The return value for the function is a CodaServerConnection object on success or a boolean false on failure.

Unlike MySQL, CodaServer does not expect you to create a new connection with every page request. This is because CodaServer has different physical logins for every user rather than a single user that all website visitors share. It is recommended that you store this Connection object in the session or in some other persistent place as you will need it to run any subsequent commands.

Setting the Application

To set your session's application, run the following command:

codaserver_set_application($connection, 'application', 'environment');

This will enable you to run application-specific DML statements and metadata lookups.

Running a Query

$result = codaserver_query($connection, 'select ...');

The function returns a boolean false if there were errors or a resultset array on success. If there were errors, you can run the statement:

$errors = codaserver_errors();

to get an array saying what they were.

CodaClient for PHP has several ways to iterate over a resultset:

  • codaserver_fetch_row(): returns the next row as a numerically-indexed array.
  • codaserver_fetch_assoc(): returns the next row as an associative array.
  • codaserver_fetch_array(): returns the row so you can look up values either numerically or by name.

The iteration itself looks like this:

while ($row = codaserver_fetch_row($result) {
     // .. do something

You can also get a listing of the column headers with the codaserver_fetch_fields() function.

It is worth mentioning that CodaServer resultsets are really just arrays themselves. You can make use of any of PHP's array functions on them to do some pretty cool things.

Running a Command

$success = codaserver_query($connection, 'insert ...');

The return value is a boolean true on success and false on failure.

Other CodaClient Functions

In addition to the basic functions highlighted above, CodaClient for PHP has wrappers for most of CodaServer's metadata commands. These include things like polling for a user's permissions, getting table structures, and getting the proper order of form statuses. You can read more about CodaServer's metadata commands here and read about CodaClient for PHP's wrappers in the documentation.