2

I want to access randomly to a result sets retuned from a Stored Procedure using PDO Mysql and PHP. I found PDOStatement::nextRowset but the access to the result sets seems to be sequential.

EDIT I'm looking for some like this:

$pdo = new PDO("mysql:host=$server;port=$port;dbname=$dbname;charset=utf8", $user, $pass, array(PDO::ATTR_PERSISTENT => false));
$statement  = "CALL FooSP()";
$query = $pdo->prepare($statement);
$query->execute();

$query->resultSet[1][0]["ColumnFoo"] // Second Resultset, first row, column "ColumnFoo"
$query->resultSet[3][1]["ColumnBar"] // third Resultset, second row, columna "ColumnBar"
$query->resultSet[0][0]["Column1"] // first Resultset, first row, columna "Column1"

Can anyone help me?

Cristhian Boujon
  • 4,060
  • 13
  • 51
  • 90

1 Answers1

0

If You need all resultsets - just prefetch them using next rowset like this:

<?php
$sql = 'CALL multiple_rowsets()';
$stmt = $conn->query($sql);
$fetched_rowsets = array();
do {
    $rowset = $stmt->fetchAll(PDO::FETCH_NUM);
    if ($rowset) 
    {
        $fetched_rowsets[] = $rowset;
    }
# This is important part. 
} while ($stmt->nextRowset());
#Now You got the table with all data from all resultsets, fetched in PHP memory.
$fetched_rowsets[1][0]["ColumnFoo"];
$fetched_rowsets[3][1]["ColumnBar"];
$fetched_rowsets[0][0]["Column1"];
?>

Just remember that it can be memory consuming.

T.Z.
  • 2,092
  • 1
  • 24
  • 39