Connecting To SAP HANA Using PHP ODBC

As of writing, there is no native PHP client library to connect directly to HANA DB. For those wanting to develop PHP applications, you will need to use ODBC. Installing and configuring the PHP ODBC library and the HANA ODBC client library on Ubuntu is very simple. This article assumes, you are installing on Ubuntu 16 using PHP 7.

Install PHP ODBC


First ensure that the ODBC driver for PHP is installed. You can do this by executing the command below in a terminal.

root@workstation: php --info | grep odbc
If the output is empty, it means the ODBC driver is not installed. You can install the driver from the terminal by executing the following command.

root@workstation: apt-get install php7.0-odbc
You will need to restart apache, once restarted, execute the following commands again.

root@workstation: php info | grep odbc
If the driver was installed successfully, you should notice entries for ODBC. Look at the PDO drivers and ensure ODBC is available.

Download HANA Client Library


The final step is to install the HANA ODBC client library. The easiest way to do this, is to download HANA Express. On the download manager screen, select Clients (Linux X86/64). Be sure to select the Binary Installer from the Image dropdown list. Once downloaded unzip and extract the folder contents. The folder should include the hdbsetup script. Execute this script, to invoke the client setup wizard. Take note of the location where the client is being installed.

After the installation is complete, extract the contents of ODBC.TGZ located in the client folder to a suitable location. The ODBC.TGZ contains the libodbcHDB.so driver.

Sample PDO Connection


With the PHP ODBC and HANA client library installed, we can now write a basic PHP script to test the connection. The quickest way to do this, is to refer to the HANA ODBC driver within the connection string.

Listing 1

$driver = "/path/to/HANA/driver/libodbcHDB.so";
$host = "";
$uid = "";
$pwd = "";

$pdo = new PDO("odbc:Driver=$driver;ServerNode=$host; Uid=$uid;Pwd=$pwd;");

$stm = $pdo->query('SELECT NOW() AS "now" FROM DUMMY');

$obj = $stm->fetch(PDO::FETCH_OBJ);

echo $obj->now;

The sample code above executes a simple SQL query that returns the current date and time as an object.

Troubleshooting


Check that you have the most recent version of unixODBC installed. The version should be at least 2.3 or higher. To check the current version, use the following command.

root@workstation: isql --version

HANA DB

Rust

Java

SAP Business One

Node.js