Configuring PHP to work with DB2

I have been using PHP for a while now and I simply love it !  Its versatile and easy to use nature makes it attractive for new application. When I say PHP, people think it as a non robust , non scalable application service. In actuality, its the other way around, its very robust, feature rich and very scalable.  It can run on native Apache, IBM HTTP Server (IBM’s version of apache), IIS, Tomcat and many other flavors out there.  Until a few days ago, I have been using PHP with its long time love – MySQL.  PHP and MySQL integrates very easily, but there is no reason why PHP will not work with any other type of database.  Few months ago, I decided to do a project using PHP and DB2.  And to my surprise,  setting up PHP to use DB2 was as easy as setting it up against MySQL, after all database is just a database and database drivers are just database drivers.   

I am sure everybody knows how to setup PHP with MySQL and there are hundreds of document out there to explain it, but I didn’t find a whole lot for DB2, so guess what I decided to put one together.

Components required :

1. Apache Server

2. PHP

3. Zend development

4. DB2 V9.5 express c edition (free). You can use any edition of DB2 as long as you have a license for it.

If you are too lazy to download each of them (Apache/ PHP/ Zend) and configure you can use  WAMP .  I used WAMP module from Bitnami, which is Apache, MySQL and PHP that runs on Windows.  It also includes the Zend module as part of it.

Here is what I did :

1. Download WAMP from Bitnami

2. Download DB2 V9.5 Express – C Edition

3. PECL4Win module 

Once I downloaded all of them. I installed Bitnami under the C:\Bitnami directory.  I kept the directory structure simple, so its easy to use the command line and easy if I had to type the path of the installation in configuration files.  Its pretty straight forward to install Bitnami. When you install Bitnami, it will prompt for a password, this is the password for the MySql server. Type in a password that you will remember. You would need this to configure MySql server or connect to it using tools.  If you have firewall software installed, it might prompt you few time asking for permission. Hit Allow / Unblock to let the installer do its thing. 

Now lets install the DB2. Unzip the downloaded file. Then select db2setup to start the db2 install.  Choose typical install to make it easy, unless you like to go thru custom install and pick and choose what you want.  DB2 install is very straight forward. Just for information the default instance name is DB2 and the port DB2 will listen is 50000.  If you choose custom install , the installer will give you an option to change it or you can change it after you install. Refer to DB2 administration guide for details. 

Now the last step, setting up the PHP extension to use the db2 drivers. Unzip the PECL file you downloaded.  Copy the php_ibm_db2.dll  and php_pdo_odbc file to  C:\Bitnami\PHP\ext\    Now edit the C:\Bitnami\PHP\php.ini file  and add the following entries:

extension_dir = “C:/BitNami/php/ext”

extension=php_ibm_db2.dll

extension=php_pdo.dll

entension=php_pdo_odbc.dll

Once you have done all that, its time to recycle apache server and restart db2.  Its not required to restart db2, but it wouldn’t hurt at this time.  Now you are ready to code php application that access DB2. Here is a sample application that I wrote that you can use to test. You may have to modify the database name, hostname, username and password. 

Now instead of Bitnami, you can also use the Zend core V2.5 to use php on DB2. 

 


About rpillai

I am a technology enthusiasts and love to work with databases and other technology. Learning new things everyday and don't think the path ever ends ...
This entry was posted in Applications, db2 and tagged , , , , , , , , , , , , , , . Bookmark the permalink.