Jan Schreiber - Blog - About

jan.bio

Accessing IBM DB2 V7.1 from PHP4 on Debian Linux 3.0 (woody)

Abstract: This document describes how to access DB2 from the PHP scripting language on woody via the unixODBC driver.

I'm using unixODBC to access DB2 from PHP. The installation is quite easy, because one doesn't have to recompile the PHP4 package. The php4-4.1.2-4 package was already build with --with-db2. This is how you can do it:

As root install the packages unixodbc and php4-odbc:

hedvig:~# apt-get install unixodbc
hedvig:~# apt-get install php4-odbc

Then check that your /etc/php4/apache/php.ini file contains the following line:

extension=odbc.so

Make sure that /usr/IBMdb2/V7.1/lib is listed in your /etc/ld.so.conf. Run ldconfig if you made any changes in this file. Then add the following to your /etc/odbcinst.ini file (I found this on the external linkunixODBC website):

[DB2]
Description = DB2 Driver
Driver = /usr/IBMdb2/V7.1/lib/libdb2.so
FileUsage = 1
DontDLClose = 1

unixODBC now uses the ODBC driver from the DB2 library. To gain access to a database, you now have to add a datasource. To do this, edit /etc/odbc.ini:

[develop]
Description = Test to DB2
Driver = DB2
DMEnvAttr = SQL_ATTR_UNIXODBC_ENVATTR={DB2INSTANCE=db2inst1}

In my example was the name of the database I wanted to access develop. This code, again, was taken from the external linkunixODBC website.

That's it. Restart your webserver with /etc/init.d/apache restart. You can now use the odbc_connect(string DSN, string user, string passwort [, int cursor_option]) function to connect to DB2. DSN is the name of the database that you just specified in your /etc/odbc.ini file.

If you need some help on installing DB2 on woody, refer to the previous section.

I hope that you find this little HOWTO useful. Please send your suggestions to my email address below. Note that this document is provided AS IS and WITHOUT ANY WARRANTY. It is published under the GNU free documentation license.