Fix: Replace fetchAll() + foreach with a while + fetch() loop.
PDO streams one row at a time, keeping memory usage constant regardless of table size.
$stmt = $pdo->prepare('SELECT * FROM largeTable');
$stmt->execute();
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
// manipulate the data here