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:
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);
?>