CosmoCode
  • Great software.

  • Bright people.

  • Happy customers!

CosmoCode GmbH
  • Home
  • Skills
  • About Us
  • References
  • Blog
  • Open Source
←
All blogposts
→

Accessing a DB2 from PHP

I spent most of yesterday trying to access a remote DB2 instance on a AS/400 eServer iSeries System i system. Trying ibm_db2 My first approach was to use the ibm_db2 PECL extension. Should be just a pecl install ibm_db2 and you're ready to go. Except that this extension needs some libraries and headers from DB2 to compile.

Andreas Gohr, 09/14/2010 10:20 a.m.

Accessing a DB2 from PHP

I spent most of yesterday trying to access a remote DB2 instance on a AS/400 eServer iSeries System i system1).

Trying ibm_db2

My first approach was to use the ibm_db2 PECL extension. Should be just a

pecl install ibm_db2

and you're ready to go. Except that this extension needs some libraries and headers from DB2 to compile.

I'll spare you the description of my various tries to find the correct download at IBM's website. The thing that finally worked for me was to download the whole DB2 Express-C database package (408MB).

When you install the whole thing, make sure you choose a “Custom” installation and tick the “Application Development” box. Otherwise you'll not get the needed header files.

After that, you can compile the PECL extension and add it to your php.ini. However it will only run when the correct environment variables are set for your PHP interpreter (eg. by sourcing /home/db2inst/sqllib/db2profile). Alternatively there's a php.ini option to set the path to your DB2 user.

Why this has to be done even when you don't plan to use a local DB2 is beyond me.

Connection Parameters

Once I had the PHP extension running I tried to connect to the DB2 at the AS/400 machine.

The manual explains that you can either use a “cataloged connection” or provide a full DSN to the DB in question.

As far as I understand, you can cataloge a remote database in your own, local DB2 setup and then connect to this alias. Since I tried to avoid the whole local DB2 stuff as much as possible, I went with the “uncataloged” option of providing a DSN.

Here's is what my final connect call looked like:

<?php
$handle = db2_connect('DATABASE=S123456E;HOSTNAME=192.1.1.55;PORT=446;PROTOCOL=TCPIP;UID=user;PWD=pass',null,null);
if($handle === false){
    die(db2_conn_errormsg());
}

And here is what I got back:

SQL8002N An attempt to connect to a host failed due to a missing DB2 Connect product or invalid license.

Turned out that this connector seems not to be allowed to talk to DB2 installs running on Series i machines :-/.

Back to the drawing board.

UnixODBC to the Rescue

ODBC is pretty common on Windows Servers but not so much in the Linux world, so this is why it was my second choice. Turned out it is the far easier and more straight forward way.

First of all you need to install UnixODBC and the PHP ODBC extension. Both should be available in your Linux distribution of choice.

Next you need the correct ODBC drivers from IBM. The package is called iSeries Access2).

It's an rpm download only, so for non-RPM distributions you need to unpack the package yourself and move the files to the correct place:

rpm2cpio iSeriesAccess-6.1.0-1.2.i386.rpm | cpio -idmv
mv opt/ibm /opt/

Next, the installed libraries need to made known to the system:

echo "/opt/ibm/iSeriesAccess/lib/" >> /etc/ld.so.conf
ldconfig

And finally you need to register the driver with the ODBC system:

odbcinst -i -d -f /opt/ibm/iSeriesAccess/unixodbcregistration

That's it. Accessing the DB from within PHP now works like this3):

<?php
$handle = odbc_connect('DRIVER={iSeries Access ODBC Driver};DATABASE=S123456E;SYSTEM=192.1.1.55;HOSTNAME=192.1.1.55;PORT=446;PROTOCOL=TCPIP','user','pass');
if($handle === false){
    die('failed to connect');
}

Of course you could also setup a ODBC instance alias, but I prefer to have my config in the application.

Summary

If you need to access a DB2 with PHP (from a Linux system) go with UnixODBC and don't waste your time with the native access.

1) IBM loves to rename their stuff
2) until IBM renames it again
3) I'm not sure what the SYSTEM parameter is needed for, but it won't work without

Read more

  • Daten Dashboard für Philip-Morris
  • Palava 1.0 released
  • Launch TheLabelFinder.com
  • Palava2 als PHP Bridge
  • Setting up Apache2, SuExec, PHP5/FastCGI
  • PHP 5.3 Was ist neu?
  • Calculate a Destination Coordinate based on Distance and Bearing in PHP
  • Redesign + Relaunch TheLabelfinder

Contact

Thank you for your interest!
Get in contact:

CosmoCode GmbH

Prenzlauer Allee 36G
10405 Berlin

Phone: +49 30 814 50 40 70

Fax: +49 30 2809 7093


mail: info@cosmocode.de

CosmoCode GmbH  
   

© CosmoCode 2021 | Imprint | Data Privacy | Cookies verwalten

Close
Deutsch English
  • Jobs