Fetching rows or columns from result sets in PHP (ibm_db2)

When you run a statement that returns one or more result sets, use one of the functions available in the ibm_db2 extension to iterate through the returned rows of each result set. If your result set includes columns that contain large data, you can retrieve the data on a column-by-column basis to avoid large memory usage.

Before you begin

You must have a statement resource returned by either the db2_exec or db2_execute function that has one or more associated result sets.

Procedure

To fetch data from a result set:

  1. Fetch data from a result set by calling one of the fetch functions.
    Table 1. ibm_db2 fetch functions
    Function Description
    db2_fetch_array Returns an array, indexed by column position, representing a row in a result set. The columns are 0-indexed.
    db2_fetch_assoc Returns an array, indexed by column name, representing a row in a result set.
    db2_fetch_both Returns an array, indexed by both column name and position, representing a row in a result set
    db2_fetch_row Sets the result set pointer to the next row or requested row. Use this function to iterate through a result set.
    db2_fetch_object Returns an object with properties representing columns in the fetched row. The properties of the object map to the names of the columns in the result set.
    These functions accept the listed arguments:
    stmt
    A valid statement resource.
    row_number
    The number of the row that you want to retrieve from the result set. Row numbering begins with 1. Specify a value for this optional parameter if you requested a scrollable cursor when you called the db2_exec or db2_prepare function. With the default forward-only cursor, each call to a fetch method returns the next row in the result set.
  2. Optional: If you called the db2_fetch_row function, for each iteration over the result set, retrieve a value from the specified column by calling the db2_result function. You can specify the column by either passing an integer that represents the position of the column in the row (starting with 0), or a string that represents the name of column.
  3. Continue fetching rows until the fetch function returns False, which indicates that you have reached the end of the result set.

    For more information about the ibm_db2 extension, see http://www.php.net/docs.php.

Example

Example 1: Fetch rows from a result set by calling the db2_fetch_object function

<?php
$conn = db2_connect("sample", "db2inst1", "password");
$sql = 'SELECT FIRSTNME, LASTNAME FROM EMPLOYEE WHERE EMPNO = ?';
$stmt = db2_prepare($conn, $sql);
db2_execute($stmt, array('000010'));
while ($row = db2_fetch_object($stmt)) {
  print "Name: 
  {$row->FIRSTNME} {$row->LASTNAME}

  ";
  }
db2_close($conn);
?>

Example 2: Fetch rows from a result set by calling the db2_fetch_row function

<?php
$conn = db2_connect("sample", "db2inst1", "password");
$sql = 'SELECT FIRSTNME, LASTNAME FROM EMPLOYEE WHERE EMPNO = ?';
$stmt = db2_prepare($conn, $sql);
db2_execute($stmt, array('000010'));
while (db2_fetch_row($stmt)) {
  $fname = db2_result($stmt, 0);
  $lname = db2_result($stmt, 'LASTNAME');
  print "
  Name: $fname $lname
  
  ";
  }
db2_close($conn);
?> 

Example 3: Fetch rows from a result set by calling the db2_fetch_both function

<?php
$conn = db2_connect("sample", "db2inst1", "password");
$sql = 'SELECT FIRSTNME, LASTNAME FROM EMPLOYEE WHERE EMPNO = ?';
$stmt = db2_prepare($conn, $sql);
db2_execute($stmt, array('000010'));
while ($row = db2_fetch_both($stmt)) {
  print "
  NAME: $row[0] $row[1]

  ";
  print "
  NAME: " . $row['FIRSTNME'] . " " . $row['LASTNAME'] . "

  ";
  }
db2_close($conn);
?> 

What to do next

When you are ready to close the connection to the database, call the db2_close function. If you attempt to close a persistent connection that you created by using db2_pconnect, the close request returns TRUE, and the IBM® data server client connection remains available for the next caller.