close

 

例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

arrow
arrow
    全站熱搜

    stockwfj3 發表在 痞客邦 留言(0) 人氣()