PHP 8.0.26 Released!


(PECL ibm_db2 >= 1.0.0)

db2_fetch_row Sets the result set pointer to the next row or requested row


db2_fetch_row(resource $stmt, int $row_number = ?): bool

Use db2_fetch_row() to iterate through a result set, or to point to a specific row in a result set if you requested a scrollable cursor.

To retrieve individual fields from the result set, call the db2_result() function.

Rather than calling db2_fetch_row() and db2_result(), most applications will call one of db2_fetch_assoc(), db2_fetch_both(), or db2_fetch_array() to advance the result set pointer and return a complete row as an array.



A valid stmt resource.


With scrollable cursors, you can request a specific row number in the result set. Row numbering is 1-indexed.


Returns true if the requested row exists in the result set. Returns false if the requested row does not exist in the result set.


示例 #1 Iterating through a result set

The following example demonstrates how to iterate through a result set with db2_fetch_row() and retrieve columns from the result set with db2_result().

'SELECT name, breed FROM animals WHERE weight < ?';
$stmt db2_prepare($conn$sql);
db2_execute($stmt, array(10));
while (
db2_fetch_row($stmt)) {
$name db2_result($stmt0);
$breed db2_result($stmt1);
"$name $breed";


cat Pook
gold fish Bubbles
budgerigar Gizmo
goat Rickety Ride

示例 #2 i5/OS recommended alternatives to db2_fetch_row/db2_result

On i5/OS it is recommended that you use db2_fetch_both(), db2_fetch_array(), or db2_fetch_object() over db2_fetch_row()/db2_result(). In general db2_fetch_row()/db2_result() have more issues with various column types in EBCIDIC to ASCII translation, including possible truncation in DBCS applications. You may also find the performance of db2_fetch_both(), db2_fetch_array(), and db2_fetch_object() to be superior to db2_fetch_row()/db2_result().

$stmt db2_exec($conn$sql, array('cursor' => DB2_SCROLLABLE));
  while (
$row db2_fetch_both($stmt)){
"<br>db2_fetch_both {$row['SPECIFIC_NAME']} {$row['ROUTINE_CREATED']} {$row[5]}";
$stmt db2_exec($conn$sql, array('cursor' => DB2_SCROLLABLE));
  while (
$row db2_fetch_array($stmt)){
"<br>db2_fetch_array {$row[1]}  {$row[5]}";
$stmt db2_exec($conn$sql, array('cursor' => DB2_SCROLLABLE));
  while (
$row db2_fetch_object($stmt)){
"<br>db2_fetch_object {$row->SPECIFIC_NAME} {$row->ROUTINE_CREATED}";


db2_fetch_both MATCH_ANIMAL 2006-08-25- 2006-08-25-
db2_fetch_both MULTIRESULTS 2006-10-17- 2006-10-17-
db2_fetch_array MATCH_ANIMAL 2006-08-25-
db2_fetch_array MULTIRESULTS 2006-10-17-
db2_fetch_object MATCH_ANIMAL 2006-08-25-
db2_fetch_object MULTIRESULTS 2006-10-17-


  • db2_fetch_array() - Returns an array, indexed by column position, representing a row in a result set
  • 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_object() - Returns an object with properties representing columns in the fetched row
  • db2_result() - Returns a single column from a row in the result set

add a note

User Contributed Notes 2 notes

krisdover at hotmail dot com
15 years ago
if the second parameter (the row number) is specified, your connection needs to have the CURSOR option set to DB2_SCROLLABLE. Otherwise all calls to this function will fail. Internal to the ibm_db2 extension module the db2cli api function SQLFetchScroll() generates the error "CLI0145E  Fetch type out of range" since it requires a scrollable resultset to work, instead of the default forward only resultset.

hope this saves someone the time it took me to track this down in the db2cli traces.

Kris Dover
andrey at php dot net
17 years ago
If the second parameter is 0 or NULL (which eventually evaluates to 0) then the internal pointer is moved forward. < 0 value will return an warning.
To Top