Over a million developers have joined DZone.
Platinum Partner

NuoDB for PHP

· Web Dev Zone

The Web Dev Zone is brought to you in partnership with Mendix.  Discover how IT departments looking for ways to keep up with demand for business apps has caused a new breed of developers to surface - the Rapid Application Developer.

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 in partnership with Mendix.  Learn more about The Essentials of Digital Innovation and how it needs to be at the heart of every organization.


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