【问题标题】:Reset cursor position in PDO在 PDO 中重置光标位置
【发布时间】:2013-02-13 05:39:41
【问题描述】:
$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 中的光标位置?

【问题讨论】:

  • 如果不需要两次取数据,那为什么还要重置光标位置呢?
  • @YourCommonSense 你肯定错过了我的意思。我想知道如何在 PDO 中重置光标。 Downvote 完全没有道理。

标签: php mysql pdo


【解决方案1】:

AFAIK 无法使用 PDO 重置光标位置 - 这可能与某些不支持重置内部光标的数据库的兼容性有关。

如果您想对结果进行两次迭代,请将其获取到数组并迭代此数组:

<?php 
$results = $stmt->fetchAll();  
foreach($results as $row) {
    // first
}

foreach($results as $row) {
    // second
}

编辑 一些数据库支持可滚动游标。要使用它,请将PDO::CURSOR_SCROLL 标志添加到prepare 方法(参见PDOFetch documentation page 的示例)。但这只会增加向前或向后移动的可能性,而不是完全倒带。此外,并非所有数据库都支持这种类型的游标(例如 MySQL 不支持)。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-03
  • 2012-04-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多