When a single call to a stored procedure returns more than
one result set, you can use the PDOStatement::nextRow method
of the PDO API to retrieve the result sets.
Before you begin
You
must have a PDOStatement object returned by calling a stored procedure
with the PDO::query or PDOStatement::execute method.
Procedure
To retrieve multiple result sets:
- Fetch rows from the first result set returned from the
procedure by calling one of the PDO fetch methods. (The first result
set that is returned from the procedure is associated with the PDOStatement
object returned by the CALL statement.)
- To return a single row from a result set as an array or object,
call the PDOStatement::fetch method.
- To return all of the rows from the result set as an array
of arrays or objects, call the PDOStatement::fetchAll method.
Fetch rows from the PDOStatement object until no more rows are
available in the first result set.
- Retrieve the subsequent result sets by calling the PDOStatement::nextRowset method
to return the next result set. You can fetch rows from the PDOStatement
object until no more rows are available in the result set.
The PDOStatement::nextRowset method
returns False when no more result sets are available or the procedure
did not return a result set.
For more information about the
PDO API, see http://php.net/manual/en/book.pdo.php.
Example
Retrieve multiple result sets from a stored procedure.
$sql = 'CALL multiple_results()';
$stmt = $conn->query($sql);
do {
$rows = $stmt->fetchAll(PDO::FETCH_NUM);
if ($rows) {
print_r($rows);
}
} while ($stmt->nextRowset());
What to do next
When
you are ready to close the connection to the database, set the PDO
object to NULL. The connection closes automatically when the PHP script
finishes.