DB2 10.5 for Linux, UNIX, and Windows

Testing the PHP database connectivity with the db2dsdriver.cfg file

You can test the PHP database connectivity with the db2dsdriver.cfg file to ensure that the db2dsdriver.cfg file is valid and the PHP driver is installed correctly.

Before you begin

You must ensure that the IBM® data server client or IBM Data Server Driver Package software is installed and the db2dsdriver.cfg file is created in your environment with the required data source name (DSN) entry. You can use the contents of the db2dsdriver.cfg.sample file to create the db2dsdriver.cfg file.

You must have a working PHP environment. For more information, see Setting up the PHP environment.

You must install the PHP driver before testing the connection. To install the PHP driver:
  1. You must determine whether your PHP environment is threadsafe or thread unsafe by issuing the following command:
    $ php -info| grep "Thread Safe"
  2. The IBM data server client and IBM Data Server Driver Package software are shipped with two types of PHP drivers:
    • Threadsafe: ibm_db2_XX_ts.so and pdo_ibm_XX_ts.so
    • Thread unsafe: ibm_db2_XX_nts.so and pdo_ibm_XX_nts.so
    Copy the appropriate PHP driver share library files to the installed PHP extension directory. For a 32-bit PHP driver:
    $ cp <DB2_Install_Path>/php/php32/ibm_db2_XX_[ts/nts].so <local_php_directory>/php/lib/php/extensions/
    $ cp <DB2_Install_Path>/php/php32/pdo_ibm_XX_[ts/nts].so <local_php_directory>/php/lib/php/extensions/
    For a 64-bit PHP driver:
    $ cp <DB2_Install_Path>/php/php64/ibm_db2_XX_[ts/nts].so <local_php_directory>/php/lib/php/extensions/
    $ cp <DB2_Install_Path>/php/php64/pdo_ibm_XX_[ts/nts].so <local_php_directory>/php/lib/php/extensions/
    The <DB2_Install_Path> is the installation path of the IBM data server client and the IBM Data Server Driver Package software. The <local_php_directory> is the PHP installation path.
  3. Edit the extension entry in the php.ini file in the <local_php_directory>/php/lib path to reference the PHP driver. For the threadsafe PHP environment:
    extension=ibm_db2_XX_ts.so
    extension=pdo_ibm_XX_ts.so
    For the thread unsafe PHP environment:
    extension=ibm_db2_XX_nts.so
    extension=pdo_ibm_XX_nts.so

You can also manually compile and install the PHP driver. For more information, see Setting up the PHP environment on Linux or UNIX.

Procedure

To test the PHP connectivity with the db2dsdriver.cfg file:

  1. Change the current path to the <DB2_Install_Path>/bin path.
  2. Issue the php validate_install.php <dsn_name> <username> <password> command. The <dsn_name> is the DSN in the db2dsdriver.cfg file that you are testing the connection to. The <username> is the valid user ID that is used to connect to the target data source. The <password> is the valid password that is associated with the specified user ID.
  3. Optional: You can also test the PHP connection without the db2dsdriver.cfg file with the php validate_install.php <database_name> <host_name> <port> <username> <password> command. The <database_name> is database name that you are testing the connection to. The <host_name> is the host name of the server where the target database resides. The <port> is the database listener port that is configured on the database server. The <username> is the valid user ID that is used to connect to the target data source. The <password> is the valid password that is associated with the specified user ID.

Results

The php validate_install.php <dsn_name> command output displays the connection test status for the pdo_ibm and ibm_db2 PHP drivers.
If you receive the following error message, you have a 32-bit PHP environment but you configured the 64-bit IBM Data Server driver:
Unable to load dynamic library ibm_db2_x.x.x_ts.so: wrong ELF class: ELFCLASS32:
To configure the 32-bit IBM data server driver:
  1. Run or source the 32-bit profile script file. If you installed the 64-bit IBM Data Server Driver Package software:
    • In the 32-bit Bash or Korn shell environment, run the db2profile32 file.
    • In the 32-bit C shell environment, source the db2cshrc32 file.
  2. Copy the appropriate PHP driver share library files to the installed PHP extension directory. For a 32-bit PHP driver:
    $ cp <DB2_Install_Path>/php/php32/ibm_db2_XX_[ts/nts].so <local_php_directory>/php/lib/php/extensions/
    $ cp <DB2_Install_Path>/php/php32/pdo_ibm_XX_[ts/nts].so <local_php_directory>/php/lib/php/extensions/

Example

The following command output shows a successful connection to the sampledsn DSN entry:
$ php validate_install.php sampledsn username password
Connection to database sampledsn, using ibm_db2 module, is successful.
Connection to database sampledsn, using pdo_ibm module, is successful.
$ php validate_install.php sample samplehost.domain.com 50000 username password
Connection to database sample, using ibm_db2 module, is successful.
Connection to database sample, using pdo_ibm module, is successful.