I am writing a batch script and get a Allowed memory size of 134217728 bytes exhausted error.
I don’t understand why the memory is filling up. I tried unsetting the $row variable, but that didn’t change a thing. Here is my code:
// ... (sql connection)
$result = mysql_query("SELECT * FROM large_table");
while ($row = mysql_fetch_array($result)) {
echo $row['id'] . PHP_EOL;
unset($row);
}
(simplified code)
Why does the memory fill up, and how can I avoid it?
Note: this is a batch script. This is normal that I have to handle data like that (go through 1 million lines).
Update: The out of memory happens around the 400 000th line, so this has got to be something in the loop? I’d like to avoid having to implement the paging if possible.
Try using http://www.php.net/manual/en/function.mysql-unbuffered-query.php (mysql_unbuffered_query()) to prevent the whole table being loaded into memory, but still avoiding pagination.