我想使用 PDO Mysql 和 PHP 随机访问从存储过程返回的结果集。我找到了 PDOStatement::nextRowset但对结果集的访问似乎是顺序的。
编辑 我正在寻找这样的东西:
$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"
谁能帮帮我?
最佳答案
如果您需要所有结果集 - 只需使用 next rowset 预取它们像这样:
<?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"];
?>
请记住,它可能会消耗内存。
关于php - 使用 PDO Mysql 和 PHP 随机访问行集/结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26498118/