How can I get a iterable resultset from the database using pdo, instead of a large array?

Posted by Tchalvak on Stack Overflow See other posts from Stack Overflow or by Tchalvak
Published on 2010-04-22T20:30:33Z Indexed on 2010/04/22 20:33 UTC
Read the original article Hit count: 225

Filed under:
|
|
|
|

I'm using PDO inside a database abstraction library function query.

I'm using fetchAll(), which if you have a lot of results, can 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 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); 
}

© Stack Overflow or respective owner

Related posts about pdo

Related posts about database