Note that fetchAll() can be extremely memory inefficient for large data sets. My memory limit was set to 160 MB this is what happened when I tried:
<?php
$arr = $stmt->fetchAll();
// Fatal error: Allowed memory size of 16777216 bytes exhausted
?>
If you are going to loop through the output array of fetchAll(), instead use fetch() to minimize memory usage as follows:
<?php
while ($arr = $stmt->fetch()) {
echo round(memory_get_usage() / (1024*1024),3) .' MB<br />';
// do_other_stuff();
}
// Last line for the same query shows only 28.973 MB usage
?>