Since sometimes you just don’t want all of the results of a query back at once, Rob Young has posted a solution of his own using the Iterators included with PHP as a part of the SPL. His solution is to wrap it in a ChunkedQueryIterator that handles the work behind the scenes.
When executing large queries it’s usually best not to load the whole result set in one go. Memory isn’t infinite and PHP isn’t renowned for handling it very well. So the obvious answer is to chunk the large query in to lots of smaller queries. […] We want something to which we can just provide a PDO object, an SQL query and the chunk size. We should then be able to iterate over the resulting object as though it were a single result set.
To read the rest of Rob’s blog, visit http://www.phpdeveloper.org/news/13350