PHP DB2 Interface

 

  1. Identify and include DB2 specific drivers:

For PHP, DB2 support is included during the build process. Listing 1 below shows the specification for configuring DB2 support for Apache/PHP.

 
Listing 1: Using gcc to build the Apache server.
## pre-configure Apache for PHP4's configure step
cd apache_1.3.19
./configure \
--prefix=/usr/local/apache
## build PHP
cd ../php-4.0.4pl1
./configure \
--with-apache=../apache_1.3.19 \
--with-mysql \
--with-ibm-db2=/usr/IBMdb2/V7.1 \
--with-xml \
--enable-debug=yes
make
make install

    .

    .

    .

  1. Connect to DB2 database 

DB2 support in PHP is implemented via ODBC call statements (see links to ODBC URL listed below). An example of connecting to a DB2 database from PHP follows;

$id = odbc_pconnect($dsn,$user,$pass);

where:

$id    = connection id returned from driver

$dsn   = DB2 database name

$user  = DB2 database user

$pass  = DB2 database user password

 

  1. Formulate  and Execute SQL statements with required parameters

With PHP, SQL requests are submitted to DB2 via ODBC statements. An example of  executing an SQL statement  to DB2  from PHP follows;

$sql_stmt="select fname, lname from contacts where id  = $id";

$result=odbc_exec ($id,$sql_stmt);

odbc_result_all($result);

where:

$sql_stmt        = variable used to hold sql statement text

$id              = connection id returned from pconnect

$result          = result id returned from odbc_exec

odbc_result_all  = displays results in HTML table format

 

  1. Disconnect from DB2 database

With PHP, SQL statements are submitted to DB2 via ODBC statements. An example of  disconnecting from a  DB2 database follows;

odbc_close($id);

where:

$id              = connection id returned from pconnect

 

Links with related information:

PHPbuilder  - for intermediate and advanced PHP developers

PHP homepage - home of PHP

DevShed  - user submitted tutorials, PHP articles

PHP ODBC - odbc homepage