Over a million developers have joined DZone.

NuoDB for PHP

· Web Dev Zone

Easily build powerful user management, authentication, and authorization into your web and mobile applications. Download this Forrester report on the new landscape of Customer Identity and Access Management, brought to you in partnership with Stormpath.

I have written a NuoDB database driver for the PHP Programming language.  The database driver conforms to the PHP Data Objects (PDO) extension API.  In this article, I’ll show you how to install, configure, and use the NuoDB PHP PDO Driver.  The instructions below are for Windows, but other NuoDB platforms are installed and configured in a similar way.

Pre-requisite 1: Installing PHP

Starting from a Windows machine, you can obtain a binary distribution of PHP fromhttp://windows.php.net/download/.  In this example, I download PHP 5.4.14 VC9 x86 Thread Safe version of PHP (php-5.4.14-Win32-VC9-x86.zip):


Then I unzip php-5.4.14-Win32-VC9-x86.zip into the C:\php directory.

Next I add C:\php to the end of PATH environment variable (Start -> Control Panel -> System -> Advanced -> Environment Variables -> System Variables -> Variable: Path).  For example:


Pre-requisite 2: Install and Configure NuoDB

At this point, check that everything is setup correctly by using nuosql  to look at the sample Hockey database:

C:\> nuosql test@localhost –user dba –password goalie –schema HOCKEY


Pre-requisite 3: Install the NuoDB PHP PDO Driver

The source code for the NuoDB PHP PDO Driver is available on GitHub, but you won’t need to build it yourself because pre-built versions of the NuoDB PHP PDO Driver are included in your NuoDB installation. You can find them in drivers\php_pdo directory of your NuoDB installation.  Example:


That directory contains two subdirectories: vc9 and vc10. Use the version that matches the Microsoft Visual C++ compiler version of your PHP executable.  You can find the version of Microsoft Visual C++ compiler that was used to build PHP by using the command php –I | findstr Compiler. For example:


Next, find the location of the extension directory that is used by PHP.  You can do that with the command php –I | findstr extension_dir. For example:


Now that you have determined the version of the Visual C++ compiler and the extension_dir, you can copy the appropriate NuoDB PHP PDO Driver version to that extension_dir.  The NuoDB PHP PDO Driver is a Windows DLL with the namephp_pdo_nuodb.dll.  For example:


Now that you have copied the NuoDB PHP PDO Driver to your PHP extension directory, you need to modify your PHP configuration to automatically load the NuoDB PHP PDO Driver when PHP runs.  Do this by adding the following line to your php.ini file:


For example:


You can use the command php -i | findstr PDO to verify that the NuoDB PHP PDO driver is loading correctly.  You should see the following output:


Using the PDO driver

Now you can run the example.php script to display information from the example “hockey” database.  Example execution output:


Let’s look at an sample application that uses a prepared statement:

try {
$db = new PDO(“nuodb:database=test@localhost;schema=Hockey”, “dba”, “goalie”) or die;
$number = 12;
$sql = “select * from HOCKEY where NUMBER < :number”;
$stmt = $db->prepare($sql);
$stmt->bindParam(‘:number’, $number, PDO::PARAM_INT);
$result = $stmt->fetchAll();
foreach ($result as $row) {
echo “NUMBER=” . $row["NUMBER"] . ” NAME=” . $row["NAME"] . “\n”;
$db = NULL;
} catch(PDOException $e) {
echo $e->getMessage();
} $db = NULL;
echo “done\n”;

The PDO connection is formed by the statement:

$db = new PDO(“nuodb:database=test@localhost;schema=Hockey”, “dba”, “goalie”)



At this point, you can use the NuoDB PHP PDO Driver just like any other PHP PDO Database Driver.

The Web Dev Zone is brought to you by Stormpath—offering a complete, pre-built User Management API for building web and mobile applications, and APIs. Download our new whitepaper: "Build Versus Buy: Customer Identity Management for Web and Mobile Applications".


Published at DZone with permission of Seth Proctor , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}