dongluo3962 2010-04-22 12:30
浏览 55
已采纳

如何使用pdo而不是大型数组从数据库中获取可迭代结果集?

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); 
}

展开全部

  • 写回答

1条回答 默认 最新

  • douxiaomang5640 2010-04-22 13:43
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部