PDOStatement::nextRowset

(PHP 5 >= 5.1.0, PHP 7, PHP 8, PECL pdo >= 0.2.0)

PDOStatement::nextRowset Advances to the next rowset in a multi-rowset statement handle

Description

public PDOStatement::nextRowset(): bool

Some database servers support stored procedures that return more than one rowset (also known as a result set). PDOStatement::nextRowset() enables you to access the second and subsequent rowsets associated with a PDOStatement object. Each rowset can have a different set of columns from the preceding rowset.

Parameters

This function has no parameters.

Return Values

Returns true on success or false on failure.

Examples

Example #1 Fetching multiple rowsets returned from a stored procedure

The following example shows how to call a stored procedure, MULTIPLE_ROWSETS, that returns three rowsets. We use a do / while loop to loop over the PDOStatement::nextRowset() method, which returns false and terminates the loop when no more rowsets can be returned.

<?php
$sql 
'CALL multiple_rowsets()';
$stmt $conn->query($sql);
$i 1;
do {
    
$rowset $stmt->fetchAll(PDO::FETCH_NUM);
    if (
$rowset) {
        
printResultSet($rowset$i);
    }
    
$i++;
} while (
$stmt->nextRowset());

function 
printResultSet(&$rowset$i) {
    print 
"Result set $i:\n";
    foreach (
$rowset as $row) {
        foreach (
$row as $col) {
            print 
$col "\t";
        }
        print 
"\n";
    }
    print 
"\n";
}
?>

The above example will output:

Result set 1:
apple    red
banana   yellow

Result set 2:
orange   orange    150
banana   yellow    175

Result set 3:
lime     green
apple    red
banana   yellow

See Also

add a note add a note

User Contributed Notes 4 notes

up
1
guilleamathews at gmail dot com
12 years ago
PDO::nextRowset() does not appear to be currently supported by the Firebird PDO driver. Unfortunate.
up
1
a dot naschekin at mypsw dot ru
5 years ago
When you call $stmt->nextRowset(); it returns TRUE every time and does not stop with FALSE even when no more results left.

It causes  Fatal Error when calling
$results[] = $stmt->fetchAll(PDO::FETCH_ASSOC);
with error message
"PHP message: PHP Fatal error:  Uncaught PDOException: SQLSTATE[HY000]: General error in ..."
unable to fetch result which not exists.

The desission is try {} catch(e) {}

            $stmt = pdo->query("Call Get_multi_result();");
            do {
                try {
                    $streets[] = $stmt->fetchAll(PDO::FETCH_ASSOC);
                } catch (PDOException $e) {
                   
                }
            } while ($stmt->nextRowset());
up
0
et dot coder at gmail dot com
9 years ago
on MSSQL and 'dsn' => 'dblib:...',:
If you know how many count rowset then don't use contruction of do..while.

<?php
do {
   
$pdoStatement->fetchAll(\PDO::FETCH_ASSOC);
} while (
   
$pdoStatement->nextRowset()
);
?>
When I get large fetchAll(over 30) for second nextRowset. I get error - Segmentation fault.

Uses step-by-step insted do..while is fix for this bug:
<?php
$pdoStatement
->fetchAll(\PDO::FETCH_ASSOC);
$pdoStatement->nextRowset();
$pdoStatement->fetchAll(\PDO::FETCH_ASSOC);
?>
up
-4
alex at 1stleg dot com
7 years ago
If you use PDO::fetch instead of PDO::fetchAll and do not reach the end the result set, PDO::nextRowset() will fail with "SQLSTATE[HY000]: General error: PDO_DBLIB: dbresults() returned FAIL."
To Top