Before you can connect to an IBM® database server and run SQL statements, you
must set up the PHP environment.
Before you begin
You
must have the following required software installed on your system:
- PHP
7.3 or later
- If
your PHP application connects to a remote IBM database, the
computer that runs your PHP application requires the IBM Data Server
Client. The PHP
application is not compatible with the following products:
- The IBM Data Server Runtime
Client
product
- The
IBM Data Server Driver Package
product
- The IBM Data Server Driver for
ODBC and CLI
product
If your PHP application connects to an IBM database
server on the local computer, no additional IBM data server
products are required.
Procedure
To install the ibm_db2 and pdo_ibm php extensions:
- Using the export command, set the environment variable
IBM_DB_HOME.
$export IBM_DB_HOME=DB2HOME
The DB2HOME is the directory where the IBM Data Server
product is installed. For example:
$ export IBM_DB_HOME=/home/db2inst1/sqllib
- Using one of the following three methods, install the ibm_db
and pdo_ibm extensions.
- Use the pecl install command included in
the PHP Extension Community Library (PECL).
- To install the ibm_db2 extension:
$ pecl install ibm_db2
- To install the pdo_ibm extension:
$ pecl install pdo_ibm
- Use the commands included in the source code:
- Extract the source archive.
- Run the following commands from the extracted directory:
$ phpize --clean
$ phpize
$ ./configure
$ make
$ make install
- If you are installing the pdo_ibm extension, you must run the following configure
command:
$./configure --with-PDO_IBM=DB2HOME
The DB2HOME
variable is the directory where the IBM Data Server product is
installed.
- Open the php.ini file in an editor of your choice. Edit the
extension entry in the php.ini file in the
<local_php_directory>/php/lib directory to reference
the PHP driver:
extension=pdo.so
extension=ibm_db2.so
extension=pdo_ibm.so
Note: Before
running a PHP script when using the ibm_db2 package, ensure that the php.ini file contains the name
of your
Db2
instance.
ibm_db2.instance_name=<name_of_the_db2instance>
You can also
source the db2profile from your instance in your PHP script. For more information, see
the PHP
requirements page.
-
Ensure that the PHP driver can access the libdb2.so CLI driver
file by setting the LD_LIBRARY_PATH variable for Linux and UNIX operating
systems other than the AIX operating system. For AIX operating system,
you must set LIBPATH variable.
- For a 32-bit Linux and UNIX operating systems other than the AIX
operating system, use the export command to set
the IBM_DB_HOME/lib32 directory
to the LD_LIBRARY_PATH variable.
export LD_LIBRARY_PATH=DB2HOME/lib32
- For a 32-bit AIX operating system, use the export command
to set the IBM_DB_HOME/lib32 directory
to the: LIBPATH variable.
export LIBPATH=DB2HOME/lib32
- For a 64-bit Linux and UNIX operating systems other than the AIX
operating system, use the export command to set
the LD_LIBRARY_PATH variable to the IBM_DB_HOME/lib64 directory.
export LD_LIBRARY_PATH=DB2HOME/lib64
- For a 64-bit AIX operating system, use the export command
to set the LD_LIBRARY_PATH variable to the IBM_DB_HOME/lib64 directory.
export LIBPATH=DB2HOME/lib64
- Optional: If the PHP application that is connecting
to an IBM database server is
running in the HTTP server environment:
- Add the LD_LIBRARY_PATH variable in the httpd.conf or
httpd.service file.
For a 32-bit architecture, set LD_LIBRARY_PATH to the DB2HOME\lib32
directory. For a 64-bit architecture set LD_LIBRARY_PATH to the DB2HOME\lib64
directory.
- Restart the HTTP server so the new configuration settings
take effect.