PDOStatement::nextRowset

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

PDOStatement::nextRowset 在一个多行集语句句柄中推进到下一个行集

说明

PDOStatement::nextRowset ( void ) : bool

一些数据库服务支持返回一个以上行集(也被称为结果集)的存储过程。PDOStatement::nextRowset() 使你能够结合一个 PDOStatement 对象访问第二个以及后续的行集。上述的每个行集可以有不同的列集合。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

Example #1 获取由一个存储过程返回的多个行集

下面例子展示了怎样调用一个存储过程,返回三个行集的 MULTIPLE_ROWSETS 。用一个 do / while 循环来循环调用 PDOStatement::nextRowset() 方法, 当不再有行集返回时返回 false 并结束循环。

<?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";
}
?>

以上例程会输出:

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

参见

User Contributed Notes

a dot naschekin at mypsw dot ru 14-Aug-2018 08:49
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());
alex at 1stleg dot com 28-Jul-2016 06:29
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."
et dot coder at gmail dot com 14-Oct-2014 01:37
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);
?>
guilleamathews at gmail dot com 19-Aug-2011 05:41
PDO::nextRowset() does not appear to be currently supported by the Firebird PDO driver. Unfortunate.