問題描述
$data=$stmt->fetchAll(); //Dumping the data shows the result. It is also setting the cursor at the end
while($data=$stmt->fetch())
{
//Does not enters loop
//If fetchAll() removes it work as usual
}
我知道它不需要兩次獲取數據.但我的主要問題是如何在 PDO 中重置光標位置?
I know It dont need to fetch data twice. But my main question is How to reset cursor position in PDO?
推薦答案
AFAIK 無法使用 PDO 重置光標位置 - 這可能與某些不支持重置內部光標的數據庫的兼容性有關.
AFAIK there is no possibility to reset cursor position with PDO - that might something to do with compatibility with some databases, that don't support resetting internal cursors.
如果你想對結果迭代兩次,把它取到數組中并在這個數組上迭代:
If you want to iterate twice over the results, fetch it to the array and iterate over this array:
<?php
$results = $stmt->fetchAll();
foreach($results as $row) {
// first
}
foreach($results as $row) {
// second
}
編輯 某些數據庫支持可滾動游標.要使用它,請將 PDO::CURSOR_SCROLL
標志添加到 prepare
方法(參見 PDOFetch 文檔頁面).但這只會增加向前或向后移動的可能性,而不是完全倒帶.此外,并非所有數據庫都支持這種類型的游標(例如 MySQL 不支持).
Edit Some databases support scrollable cursors. To use that, add PDO::CURSOR_SCROLL
flag to prepare
method (see examples at PDOFetch documentation page). But that only adds possibility to move forward or backward, not rewind completely. Also, not all databases support that type of cursor (e.g. MySQL doesn't).
這篇關于重置 PDO 中的光標位置的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!