Connect to IBM DB2 on PHP via ODBC

This was done and tested on a Linux (CentOS) system running PHP 4.

  1. Configure PHP with ODBC support.
    yum install php4-odbc unixODBC

    You should check that it is successfully installed by doing phpinfo().

  2. Obtain the latest DB2 ODBC driver from IBM. You can download it from here. Lookout for Driver for ODBC and CLI.
  3. Untar it at a dir. I chose /opt/db2cli.
  4. Edit odbcinst.ini to define a new ODBC connection. The file should be located at /etc/odbcinst.ini. Add the following:
    [DB2]
    Description     = DB2 Driver
    Driver      	= /opt/db2cli/lib/libdb2.so
    FileUsage       = 1
    DontDLClose     = 1
  5. You can now connect to IBM DB2 on PHP via ODBC. You can use the following PHP script to test the connection:
    $database = 'DBNAME';
    $hostname = 'HOST';
    $port = 'PORT';
    $user = 'USERNAME';
    $password = 'PASSWORD';
    $driver = 'DB2';
    $conn_string = "DRIVER=\{$driver};DATABASE=$database;HOSTNAME=$hostname;PORT=$port;PROTOCOL=TCPIP;UID=$user;PWD=$password;";
    
    if (!$db = odbc_connect ($conn_string, $user, $password)) echo 'Error!';
    else echo 'Success!';
    
    odbc_close($db);

You can read about more information on PHP ODBC functions via PHP’s Manual.

Though this guide is based on PHP4, this should also work for PHP5 with some minor modifications. Otherwise, you can also check out DB2 extension of PHP.

This entry was posted in Database, Tips and tagged , , , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. Posted September 2, 2011 at 22:55 | Permalink

    It’s been some time, and I’d like to understand about the bunch of ‘evil’ clowns at Ping.sg who always gave me trouble. Why do you condone such behavior??? You are at ransom, or you people are network-ed? I am just puzzled. LOL~ Rgds. If you have reply for this whch will help in my data compilation, pls drop it in my blog. (Outside notices) Thanks.

    • uzyn
      Posted September 15, 2011 at 15:04 | Permalink

      Hey. It’s been awhile. I still remember you. :) Actually I don’t know who or what you were referring to. We can talk further if you want. Email me or something.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Additional comments powered byBackType