PHP + IBM DB2 on Centos 7

Instalation of IBM DB2 module should be very easy (refering to the installation instructions):

  1. First of all, we need to download the driver package from here
  2. unpack the package to the server at /opt/ibm_db2/dsdriver
  3. chmod 755 installDSDriver
  4. ./installDSDriver
  5. Install pecl and php-pear (may need ksh): yum install php-pear
  6. pecl install ibm_db2
  7. When asked, provide the path to the db2 driver (here /opt/ibm_db2/dsdriver)
  8. You may check if it is installed by: pecl list
  9. Last thing: chmod o+w /opt/ibm_db2/dsdriver/cfg
  10. Edit /etc/php.ini and add extension=ibm_db2.so
  11. service httpd restart

Then in PHP:

<?php
    $databaseName = "your_db_name";
    $hostName = "your_host_name";
    $port = 50001;
    $userName = "your_user_name";
    $password = "your_password";
    $conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$databaseName;HOSTNAME=$hostName;PORT=$port;PROTOCOL=TCPIP;UID=$userName;PWD=$password;Security=ssl;";
    $conn = db2_connect($conn_string, '', '');

    if($conn) {
        echo "Connected!";
        db2_close($conn);
    } else {
        echo "Not connected<br>";
        echo db2_conn_errormsg();
    }
?>

Whole process with lot of pre-requisites for python is here (it works with little modifications for PHP too).