Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : IBM DB2, Cloudscape and Apache Derby Functions : db2_fetch_row

db2_fetch_row

Sets the result set pointer to the next row or requested row (PECL ibm_db2:1.0-1.6.2)
bool db2_fetch_row ( resource stmt [, int row_number] )

Example 942. 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().

<?php
$sql
= '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($stmt, 0);
   
$breed = db2_result($stmt, 1);
   print
"$name $breed";
}
?>

The above example will output:

cat Pook
gold fish Bubbles
budgerigar Gizmo
goat Rickety Ride

Example 943. 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().

<?php
 $conn
= db2_connect("","","");
 
$sql = 'SELECT SPECIFIC_SCHEMA, SPECIFIC_NAME, ROUTINE_SCHEMA, ROUTINE_NAME, ROUTINE_TYPE, ROUTINE_CREATED, ROUTINE_BODY, IN_PARMS, OUT_PARMS, INOUT_PARMS, PARAMETER_STYLE, EXTERNAL_NAME, EXTERNAL_LANGUAGE FROM QSYS2.SYSROUTINES FETCH FIRST 2 ROWS ONLY';
 
$stmt = db2_exec($conn, $sql, array('cursor' => DB2_SCROLLABLE));
 while (
$row = db2_fetch_both($stmt)){
   echo
"<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)){
   echo
"<br>db2_fetch_array {$row[1]}  {$row[5]}";
 }
 
$stmt = db2_exec($conn, $sql, array('cursor' => DB2_SCROLLABLE));
 while (
$row = db2_fetch_object($stmt)){
   echo
"<br>db2_fetch_object {$row->SPECIFIC_NAME} {$row->ROUTINE_CREATED}";
 }
 
db2_close($conn);
?>

The above example will output:

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

Code Examples / Notes » db2_fetch_row

andrey

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.

krisdover

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.
Regards,
Kris Dover


Change Language


Follow Navioo On Twitter
db2_autocommit
db2_bind_param
db2_client_info
db2_close
db2_column_privileges
db2_columns
db2_commit
db2_conn_error
db2_conn_errormsg
db2_connect
db2_cursor_type
db2_escape_string
db2_exec
db2_execute
db2_fetch_array
db2_fetch_assoc
db2_fetch_both
db2_fetch_object
db2_fetch_row
db2_field_display_size
db2_field_name
db2_field_num
db2_field_precision
db2_field_scale
db2_field_type
db2_field_width
db2_foreign_keys
db2_free_result
db2_free_stmt
db2_get_option
db2_lob_read
db2_next_result
db2_num_fields
db2_num_rows
db2_pconnect
db2_prepare
db2_primary_keys
db2_procedure_columns
db2_procedures
db2_result
db2_rollback
db2_server_info
db2_set_option
db2_special_columns
db2_statistics
db2_stmt_error
db2_stmt_errormsg
db2_table_privileges
db2_tables
eXTReMe Tracker