如何使用 pdo 而不是大型数组从数据库获取可迭代结果集?
我在数据库抽象库函数 query< 中使用 PDO /code> 我做的。
我正在使用 fetchAll()
,如果你有很多结果,应该会占用大量内存,所以我想提供一个参数来在 fetchAll 关联数组和 pdo 结果集之间切换,该结果集可以使用 foreach
进行迭代,并且需要更少的内存(以某种方式)。
我记得听说过这个,并且我搜索了 PDO 文档,但我找不到任何有用的方法来做到这一点。
有谁知道如何从 PDO 获取可迭代的结果集而不仅仅是平面数组?我认为使用可迭代的结果集会更容易占用内存,对吗?
我正在使用 Postgresql,如果在这种情况下很重要的话。
。
。
。
为了清楚起见,当前的查询函数如下。
/**
* Running bound queries on the database.
*
* Use: query('select all from players limit :count', array('count'=>10));
* Or: query('select all from players limit :count', array('count'=>array(10, PDO::PARAM_INT)));
**/
function query($sql_query, $bindings=array()){
DatabaseConnection::getInstance();
$statement = DatabaseConnection::$pdo->prepare($sql_query);
foreach($bindings as $binding => $value){
if(is_array($value)){
$statement->bindParam($binding, $value[0], $value[1]);
} else {
$statement->bindValue($binding, $value);
}
}
$statement->execute();
// TODO: Return an iterable resultset here, and allow switching between array and iterable resultset.
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
I'm using PDO inside a database abstraction library function query
that I've made.
I'm using fetchAll()
, which if you have a lot of results, is supposed to get memory intensive, so I want to provide an argument to toggle between a fetchAll associative array and a pdo result set that can be iterated over with foreach
and requires less memory (somehow).
I remember hearing about this, and I searched through the PDO docs, but I couldn't find any useful way to do that.
Does anyone know how to get an iterable resultset back from PDO instead of just a flat array? And am I right that using an iterable resultset will be easier on memory?
I'm using Postgresql, if it matters in this case.
.
.
.
The current query function is as follows, just for clarity.
/**
* Running bound queries on the database.
*
* Use: query('select all from players limit :count', array('count'=>10));
* Or: query('select all from players limit :count', array('count'=>array(10, PDO::PARAM_INT)));
**/
function query($sql_query, $bindings=array()){
DatabaseConnection::getInstance();
$statement = DatabaseConnection::$pdo->prepare($sql_query);
foreach($bindings as $binding => $value){
if(is_array($value)){
$statement->bindParam($binding, $value[0], $value[1]);
} else {
$statement->bindValue($binding, $value);
}
}
$statement->execute();
// TODO: Return an iterable resultset here, and allow switching between array and iterable resultset.
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来我已经以 PDOStatement 对象本身的形式找到了解决方案,即在执行
$statement->execute();
之后,您可以简单地传递$statement object 并 foreach 该对象。不能将它用作数组,但可以用它做几乎所有其他事情。
Looks like I've found my solution in the form of the PDOStatement object itself, i.e. after doing a
$statement->execute();
You can simply pass along the$statement
object and foreach over that object. Can't use it as an array, but can do just about everything else with it.