NoSQL Zone is brought to you in partnership with:

Seth is the CTO at NuoDB. His main areas of focus are on the administration, security and resource management models, automation and the tools that drive these pieces. Seth is a DZone MVB and is not an employee of DZone and has posted 42 posts at DZone. You can read more from them at their website. View Full User Profile

NuoDB for PHP

05.13.2013
| 1640 views |
  • submit to reddit

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):

windows.php.net.website

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:

environment_variables

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

php_cmd1

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:

php_cmd6

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:

php_cmd7

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:

php_cmd8

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:

php_cmd9

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:

extension=php_pdo_nuodb.dll

For example:

php_cmd10

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:

php_cmd11

Using the PDO driver

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

php_cmd4

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

<?php
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);
$stmt->execute();
$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”)

Example:

php_cmd13

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

Published at DZone with permission of Seth Proctor, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)