2015/08/21

Making queries with PHP 5.3 and DB2 over ODBC on top of CentOS 6.7

This is  a follow-up to my previous posting about setting up DB2 and ODBC on top of CentOS 6.7.

The pre-requirement for the setup is to have the database running on the same system. If this is not the case, download and install the DB2 ODBC drivers from IBM. The ODBC drivers of this setup have been included with the DB2 installation, so this setup uses them locally.

Please see my previous posting for contents of odbcinst.ini and odbc.ini.

 1) Install the http server, php and ODBC php package:

yum install httpd php php-odbc

2) Start up the server

service httpd start

3) Create the test application ie. /var/www/html/db2.php:

<?php
# Probably not mandatory
putenv("DB2INSTANCE=db2inst1");

$conn = odbc_connect('SAMPLE','db2inst1','[password]');

# This could be used if there is no odbc.ini

#$conn = odbc_connect('DRIVER={DB2};DATABASE=SAMPLE','db2inst1','[password]');

if($conn === false){
    die('failed to connect');
}

$qry = "SELECT * FROM EMPLOYEE";

$res = odbc_exec($conn,$qry) or die("Query error: $qry\n");

while( $row = odbc_fetch_object($res) ) {

       print_r($row);
}

odbc_close($conn);

?>


4) Access the page with a browser ie.  http://[host]/db2.php

No comments:

Post a Comment