Real logic you need to connect DB2 using PHP engine

You connect DB2 database using any high level language like Java, C#, PHP. So, my post here is how to connect DB2 in PHP engine.

You already familiar that in my earlier article, I have mentioned that there are 2 kinds of  stored procedures or functions

  1. Internal functions/Stored procedures
  2. External stored procedures/functions

In both the cases, when you are trying to access DB2 server, to run your objects like Stored procedures/functions, then, you need to write some special logic in your application program.

PHP engine

Connecting to a Database

 DB2 to PHP engine, the db2_connect, manages the connection from the PHP engine to DB2. Its general form is as follows:

db2_connect ( string connection_details, string uid, string pwd )

The following components are concatenated in the string, separated by semicolons to form the full connection string:

  • DRIVER: The name of the underlying driver used to create the connection

  • HOST: The host name for your DB2 server

  • DATABASE: The catalogued database name for the DB2 database

  • PROTOCOL: The network protocol to use for connectivity

  • PORT: For port-orientated protocols (for example, TCP/IP), the port of the DB2 listener

  • UID: The DB2 authid (user name) to use for the connection

  • PWD: The password for the user

The real code to access db2_connect in PHP is:

$driver = “{IBM DB2 ODBC DRIVER}”;
$database = “SAMPLE”;
$hostname = “localhost”;
$port = 50000;
$user = “fuzzy”;
$password = “fuzzy”;
$conn_string = “DRIVER=$driver;DATABASE=$database;”;
$conn_string .= “HOSTNAME=$hostname;PORT=$port;PROTOCOL=TCPIP;”;
$conn_string .= “UID=$user;PWD=$password;”;
$conn = db2_connect($conn_string, ”, ”);
if (! $conn)
echo db2_conn_errormsg();
echo “Hello World, from the IBM_DB2 PHP extensions!”;
catch (Exception $e) {
echo $e;



Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.

Start Discussion

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.