PHP mysql loop goes out of memory
Posted: Thu Oct 09, 2014 8:36 am
Hi everyone,
My php script stop running because it goes out of memory.
Here is my code:
How the 2nd part can stop running... its only a foreach loop, if its work once, it should work unlimited times, right ? I used all the same variables, i never keep new informations, i just use what i already have... how the memory use can increase ?
i run the script like 5 times on a csv with more than 600k rows.
And with exactly the same script it stop at different moments.
Number of request done before it stop : 205 286 // 200 514 // 192 429 // 211 164 // ...
Thank you,
Mickael.
My php script stop running because it goes out of memory.
Here is my code:
Code: Select all
$files = array_diff(scandir($dir_name), array('..', '.'));
if (!empty($files))
{
$all_lines = array();
foreach ($files as $file)
{
if (pathinfo($file, PATHINFO_EXTENSION) == "csv")
{
$fp = fopen($dir_name.$file, 'r');
while (($line = fgetcsv($fp, 0, $delimiter)) !== FALSE)
$all_lines[] = array_map('addslashes', $line);
fclose($fp);
//unlink($file);
}
}
}
/*the 1st part is done properly*/
foreach ($all_lines as $line)
{
$my_request = "INSERT INTO $table_name VALUES ('$line[0]'";
$i = 1;
while ($i < $maxcol)
{
if (isset($line[$i]))
$my_request = $my_request.", '$line[$i]'";
else
$my_request = $my_request.", ''";
$i++;
}
$my_request = $my_request.")";
if (mysqli_query($connect_id, $my_request) == FALSE)
{
echo "<p>$my_request</p>";
exit("Error while updating the table: $table_name.");
}
}
i run the script like 5 times on a csv with more than 600k rows.
And with exactly the same script it stop at different moments.
Number of request done before it stop : 205 286 // 200 514 // 192 429 // 211 164 // ...
Thank you,
Mickael.