fetchAll() loads everything into memory at once. If your table has millions of rows, PHP runs out of memory and you get the fatal error.
With fetch() in a loop you only keep one row in memory at a time. Stays low no matter how big the table gets.
$stmt = $pdo->prepare('SELECT * FROM largeTable');