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