使用PDO Mysql和PHP随机访问行集/结果集

使用PDO Mysql和PHP随机访问行集/结果集,php,mysql,pdo,rowset,Php,Mysql,Pdo,Rowset,我想使用PDO Mysql和PHP随机访问从存储过程返回的结果集。我发现,但对结果集的访问似乎是顺序的 编辑 我想找一些像这样的: $pdo = new PDO("mysql:host=$server;port=$port;dbname=$dbname;charset=utf8", $user, $pass, array(PDO::ATTR_PERSISTENT => false)); $statement = "CALL FooSP()"; $query = $pdo->prep

我想使用PDO Mysql和PHP随机访问从存储过程返回的结果集。我发现,但对结果集的访问似乎是顺序的

编辑 我想找一些像这样的:

$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"
<?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"];
?>

有人能帮我吗?

如果您需要所有结果集,只需使用以下方法预取它们:

$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"
<?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"];
?>


请记住,它可能会消耗内存

你为什么不随机排序你的查询呢?例如,
orderbyrand()
$query->resultSet[RAND(0,$query->rowCount()];
是的,您需要编辑FooSP的声明以在每次调用时返回随机结果。其他方法效率低下,因为这意味着加载您不使用的数据。不,我需要所有结果集。