是否可以倒回 PDO 结果?
我正在尝试为 PDO 语句的结果编写一个迭代器,但找不到任何回退到第一行的方法。 我想避免调用 fetchAll 和存储所有结果数据的开销。
// first loop works fine
foreach($statement as $result) {
// do something with result
}
// but subsequent loops don't
foreach($statement as $result) {
// never called
}
有什么方法可以重置语句或寻找第一行吗?
I'm trying to write an iterator for results from a PDO statement but I can't find any way of rewinding to the first row. I would like to avoid the overhead of calling fetchAll and storing all the result data.
// first loop works fine
foreach($statement as $result) {
// do something with result
}
// but subsequent loops don't
foreach($statement as $result) {
// never called
}
Is there some way of reseting the statement or seeking the first row?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我很确定这取决于数据库。 因此,您应该尽量避免这种情况。 但是,我认为您可以通过启用缓冲查询来实现您想要的目的。 如果这不起作用,您可以随时使用
获取全部
。 这两种解决方案都会影响您的应用程序性能,因此如果结果集很大,请三思。I'm pretty sure this is database dependent. Because of that, it is something you should try to avoid. However, I think you can achieve what you want by enabling buffered queries. If that doesn't work, you can always pull the result into an array with
fetchAll
. Both solutions have implications for your applications performance, so think twice about it, if the resultsets are large.我编写的这个小类包装了一个 PDOStatement。 它只存储获取的数据。 如果这不起作用,您可以移动缓存以读取和写入文件。
This little class I wrote wraps a PDOStatement. It only stores the data that is fetched. If this doesn't work, you could move the cache to read and write to a file.
请参阅此演示文稿中的幻灯片 31,您可以执行
$ statements->rewind()
(如果它适用于缓冲查询)。 如果您使用 mysql,则可以使用 PDO_MYSQL_ATTR_USE_BUFFERED_QUERY 来模拟缓冲查询:@NoahGoodrich 向您指出了 spl。 这是一个始终有效的示例:
see slide 31 from this presentation, you can do a
$statement->rewind()
if it applies to a buffered query. If you use mysql, you can emulate buffered queries by usingPDO_MYSQL_ATTR_USE_BUFFERED_QUERY
:@NoahGoodrich pointed you to spl. Here is an example that always works:
很久以前就问过,但目前有另一个解决方案。
方法
PDOStatement::fetch()
可以接收第二个参数,即光标方向,以及PDO::FETCH_ORI_*
常量之一。 仅当使用属性PDO::ATTR_CURSOR
作为PDO::CURSOR_SCROLL
创建PDOStatement
时,这些参数才有效。这样您就可以按如下方式导航。
更多信息请参阅文档和PDO 预定义常量。
注意:使用
PDO::FETCH_BOTH
因为这是默认值,只需为您的项目自定义它即可。Asked a long time ago but currently there's another solution.
The method
PDOStatement::fetch()
may receives a second parameter, the cursor orientation, with one ofPDO::FETCH_ORI_*
constants. These parameter are only valid if thePDOStatement
are created with the atributePDO::ATTR_CURSOR
asPDO::CURSOR_SCROLL
.This way you can navigate as follows.
More info in docs and PDO predefined constants.
Note: used
PDO::FETCH_BOTH
because is the default, just customize it for your project.您可能想要查看一些可以扩展以提供对对象的类似数组的访问的 PHP SPL 类。
建议您查看
ArrayIterator、ArrayObject 和
也许是迭代器接口。
教程
快速教程
You'll probably want to take a look at some of the PHP SPL classes that can be extended to provide array-like access to objects.
recommend that you look at the
ArrayIterator, ArrayObject, and
perhaps the Iterator interface.
Tutorial
Quick Tutorial