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.

18 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.

  2. Posted January 6, 2013 at 22:49 | Permalink

    It’s truly a nice and useful piece of

    info. I’m satisfied that you simply shared this helpful

    information with us. Please keep us up to date like this.
    Thanks for sharing.

  3. Posted January 7, 2013 at 01:26 | Permalink

    Hi there, i read your blog occasionally and i own
    a similar one and i was just

    curious if you get a lot of spam comments? If so how do you reduce it, any
    plugin or anything you can advise? I get so
    much lately it’s driving me

    insane so any support is very much appreciated.

  4. Posted January 7, 2013 at 10:55 | Permalink

    you’re really a good webmaster. The website loading speed is amazing. It seems that

    you are doing any unique trick. Furthermore, The contents are masterpiece. you have done a magnificent job on this topic!

  5. Posted January 7, 2013 at 19:56 | Permalink

    Hi, Neat post. There is a problem with your web site in internet explorer, would check

    this… IE still is the market leader and a huge portion of people will miss your

    fantastic writing due to this problem.

  6. Posted January 7, 2013 at 20:56 | Permalink

    Somebody essentially help to make severely articles I’d state. This is the first time I

    frequented your website page and to this point? I surprised with the

    research you made to create this particular publish

    amazing. Magnificent task!

  7. Posted January 8, 2013 at 02:02 | Permalink

    Oh my goodness! an incredible article dude.
    Thank you Nevertheless

    I’m experiencing challenge with ur rss . Don’t know why Unable

    to subscribe to it. Is there anyone getting similar rss problem? Anybody who knows kindly respond. Thnkxxx

  8. Posted January 8, 2013 at 03:36 | Permalink

    great post, very informative. I wonder why the
    other experts of

    this sector do not notice this. You should continue your writing.
    I am sure, you have a great readers’ base already!

  9. Posted January 8, 2013 at 06:31 | Permalink

    Wow that was unusual. I just wrote an incredibly long comment
    but after I clicked

    submit my comment didn’t show up. Grrrr… well I’m not writing all that over again.
    Anyway, just wanted to say superb blog!

  10. Posted January 8, 2013 at 17:33 | Permalink

    Great work! That is the type of information that are meant

    to be shared across the web. Shame on Google for now not positioning this put up higher!
    Come on

    over and talk over with my website . Thanks

    =)

  11. Posted January 8, 2013 at 22:26 | Permalink

    Would you be eager about exchanging

    hyperlinks?

  12. Posted January 9, 2013 at 14:45 | Permalink

    Hi,

    Do you have any example for Windows version?
    Thank!!

  13. Posted January 25, 2013 at 10:18 | Permalink

    I used to be suggested this web site by way of my cousin.
    I am now not sure whether this post is written through him as no
    one else know such particular approximately my problem. You’re amazing! Thank you!

  14. Posted February 11, 2013 at 02:25 | Permalink

    Pretty portion of content. I just stumbled

    upon your website and in accession capital to assert that
    I get actually loved account your blog posts. Any way I’ll be subscribing
    for your feeds or even I success you get

    right of entry to persistently rapidly.

  15. Posted May 16, 2013 at 15:31 | Permalink

    What’s up, its nice post about media print, we all know media is a fantastic source of information.

  16. Vmlpbkwf
    Posted May 19, 2013 at 03:41 | Permalink

    <a href=http://capitalplanners.net/controls/louisvuittonfactoryoutletcx.aspxlouis vuitton outlet online

  17. Posted May 20, 2013 at 10:29 | Permalink

    A Lange Sohne is to watches what Mercedes is to cars. A Lange Sohne watches have drawn out been loved and admired. If you would like a good looking vigil that will fit your own particular mode of expression as well as make any equipment that you are wearing look even better, you want an A Lange Sohne wakefulness. These watches are just as popular and just as well loved as Rolex watches. The A Lange Sohne stripe offers a broad line of styles to prefer from, but the price file is quite limited and quite requiring great outlay. If you don’t have a large parcel for a new watchfulness, you may want to consider a Duplicate A Lange Sohne outlook.

3 Trackbacks

  1. [...] the setup would be same/similar to Windows. I tried this in past and it worked .. Please check Connect to IBM DB2 on PHP via ODBC [...]

  2. By Php script installation service on September 17, 2012 at 05:06

    [...] Offers cheap, professional and fast PHP script installation service. Read More… [...]

  3. By website malware removal on February 11, 2013 at 19:24

    [...] Take a look at my web-site – http://uzyn.com/how-to-connect-to-db2-on-php-via-odbc/ Source Page [...]

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