Tuesday, July 14, 2009

iSeries Access ODBC for PHP Linux

I set up a Centos 5.2 server with PHP 5.1.6 and wanted to allow access to a AS/400 DB2 database.

First thing I did was download the iSeriesAccess driver called "iSeriesAccess-5.4.0-1.6.i386.rpm". This is also used to run a 5250/Terminal session to an AS/400, but for the purposes of this, it installs an odbc driver in "/opt/ibm/iSeriesAccess/lib/" using "libcwbodbc.so" and "libcwbodbcs.so".

Then I created a php test page to connect to the AS/400. After a days worth of trial and failure, the best advice I could find on the internet was from the phpbuilder site here.

Here's my final working example (php code):
< ? php $driver = '{iSeries Access ODBC Driver}';
$system = 'as400.company.com';
$dsn = "DRIVER=$driver;SYSTEM=$system";
$name = 'user';
$pw = 'passw0rd';

echo "Connecting to AS/400...";
$con = odbc_connect($dsn,$name,$pw);
if (!$con)
{
echo 'Error connecting: ' . odbc_error() . odbc_errormsg();
die();
}

else
{
echo "Successfully connected to $system as $name!";
odbc_close($con);
}

?>


I tried using "/etc/odbc.ini" to set up a ODBC DSN, but IBM's Redbook seemed to overcomplicate it a bit. If you need to use a DSN connection, look for IBM's Redbook "Linux Integration with IBM i5/OS" section 3.1.2.

08/06/2009 - Update - For those who have an exit point program installed on their iSeries, "SELECT" statements must be in capital letters! If you use lowercase "select" it may not work!

-Tres

No comments: