Script for testing PHP ibm_db2 functioning

NOTE! Don't forget to setup ibm_db2.instance_name=db2inst1 correctly, as guideline in PHP :)

<?php
  $db_conn
= db2_connect("DATABASE=SAMPLE;HOSTNAME=127.0.0.1;PORT=50000;PROTOCOL=TCPIP;UID=db2inst1;PWD=CHANGE", '', '');
 
$cmdstr = 'SELECT "LASTNAME", "SALARY" FROM "EMPLOYEE"';
 
$stmt = db2_prepare($db_conn, 'SELECT COUNT(*) AS "NROWS" FROM ('. $cmdstr .')');
 
db2_execute($stmt);
 
$nrows = db2_fetch_object($stmt)->NROWS;
 
$stmt = db2_prepare($db_conn, $cmdstr);
 
db2_execute($stmt);
  echo
"<html><head><title>DB2 PHP Test</title></head><body>";
  echo
"<center><h2>DB2 PHP Test</h2><br>";
  echo
"<table border=1 cellspacing='0' width='50%'>\n<tr>\n";
  echo
"<td><b>Name</b></td>\n<td><b>Salary</b></td>\n</tr>\n";
  while (
$array = db2_fetch_assoc($stmt)) {
    echo
"<tr>\n";
    echo
"<td>" . $array["LASTNAME"] . "</td>";
    echo
"<td>$ " . number_format($array["SALARY"], 2). "</td>";
    echo
"</tr>\n";
  }
  echo
"<tr><td colspan='2'> Number of Rows: $nrows</td></tr></table><br>";
  echo
"<em>If you see data, then it works!</em><br>";
  echo
"</center></body></html>\n";
?>


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <h1> <h2> <h3> <h4> <h5> <h6> <em> <strong> <code> <del> <blockquote> <q> <sub> <p> <br> <ul> <ol> <li> <dl> <dt> <dd> <a> <b> <u> <i> <sup> <acronym> <pre> <img>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Images can be added to this post.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.