Allowed memory size exhausted fix

Allowed memory size exhausted fix, while doing huge export from mysql databases if the query data exceeds the maximum then it will result in error. You can fix this issue temporairly by changing memory settings in your php.ini file or .htaccess file.

memory_limit = 128

increasing this memory limit to 256M or 512M. But instead of doing this you can fix the issue by slight change in coding algorithm. Sample pseudocode format to export the data.

Before:

       $results = mysql_query('select * from table');
	while($results){
          writedataincsv($result);
	}
        csvexpot();

After:

     $countnumber = mysql_query('select count(id) from table');
     $recordexported = 0
     while($records_exported < $countnumber){
        $results = mysql_query('select * from table limit 1000 offset $recordexported'); 
	while($results){
          writedataincsv($result);
	}
      $records_exported = $records_exported + 1000;
     }

Use this format as per your framework coding standard.

Leave a Reply

Your email address will not be published. Required fields are marked *