php - PHPExcel memory exhausted when trying to make an excel file from records in mysql database. There are upto 20000 records needed to be copied -



php - PHPExcel memory exhausted when trying to make an excel file from records in mysql database. There are upto 20000 records needed to be copied -

i have export mysql database excel file. number of records big (about 20000) when exporting database, server reported fatal error:

failed allocated 68 bytes.

every time run script, 68 bytes value changes new value 33 bytes. script is:

set_time_limit(0); error_reporting(e_all); ini_set('display_errors', '1'); ini_set("memory_limit", "1000m"); require_once("php_excel/phpexcel.php"); require_once("php_excel/phpexcel/iofactory.php"); require_once("includes/config.php"); require_once("includes/functions/functions.php"); // field name of equipments $objphpexcel = new phpexcel(); $objphpexcel->createsheet(); $objphpexcel->setactivesheetindex(0); //code display headers $activesheet=$objphpexcel->getactivesheet(); $result=exec_query("s...... "); $num_fields = mysql_num_fields($result); $x='a'; $columns=array(); $p=0; for($i=0;$i<$num_fields;$i++) { $columns[]=mysql_field_name($result, $i); $activesheet->setcellvalue($x.'1', $columns[$p]); $x++;$p++; } //second time.... $ts_id=""; $result=exec_query("........ "); while($row=fetch_array($result)) { if($ts_id!=$row['ts_id']) { $columns[]=$row['ts_id']; $activesheet->setcellvalue($x.'1', utf8_encode(explode(' ',$row['attribute_desc'])[0]).' game'); $x++;$p++; $ts_id=$row['ts_id']; } $columns[]=$row['attribute_desc']; $activesheet->setcellvalue($x.'1', $columns[$p]); $x++;$p++; $columns[]=$row['attribute_desc']; $activesheet->setcellvalue($x.'1', $columns[$p].' ok'); $x++;$p++; } //end of second.... $x='a'; $k=2; //getting attribute ids $result=exec_query("............s "); $tsid=array(); $attribute=array(); while($row=fetch_array($result)) { $attribute[]=$row['....._id']; $tsid[]=$row['ts_id']; } //getting attribute ids end... //filling values... $result=exec_query("................"); $majorwordid=array(); while($row=fetch_array($result)) { $majorwordid[]=$row['word_id']; for($i=0;$i<$num_fields;$i++) { //echo $row[$columns[$i]]."<br>"; $activesheet->setcellvalue($x.$k, utf8_encode($row[$columns[$i]])); $x++; } $x='a'; $k++; } //audio number $result=exec_query("..................."); $x='c'; $k=2; while($row=fetch_array($result)) { $activesheet->setcellvalue($x.$k, utf8_encode($row['audio number'])); $k++; } //audio number ends... //mait codesss $x++; $forid=''; for($a=0;$a<count($attribute);$a++) { $k=2; if($forid!=$tsid[$a] ||$forid=='') { $result=exec_query("............."); $word_ids=array(); while($row=fetch_array($result)) { $word_ids[]=$row['word_id']; } for($i=0;$i<count($majorwordid);$i++) { if (in_array($majorwordid[$i], $word_ids)) { $activesheet->setcellvalue($x.$k,'1'); } else $activesheet->setcellvalue($x.$k,' '); $k++; } $forid=$tsid[$a]; $x++; } $k=2; $result=exec_query("..............."); $word_ids=array(); while($row=fetch_array($result)) { $word_ids[]=$row['word_id']; } for($i=0;$i<count($majorwordid);$i++) { if (in_array($majorwordid[$i], $word_ids)) { $activesheet->setcellvalue($x.$k,'1'); } else $activesheet->setcellvalue($x.$k,' '); $k++; } // ok valuess.... $k=2; $x++; $result=exec_query("s............"); $word_ids=array(); while($row=fetch_array($result)) { $word_ids[]=$row['word_id']; } for($i=0;$i<count($majorwordid);$i++) { if (in_array($majorwordid[$i], $word_ids)) { $activesheet->setcellvalue($x.$k,'1'); } else $activesheet->setcellvalue($x.$k,' '); $k++; } $x++; } $activesheet->settitle('lexicocms'); $activesheet->getcolumndimension('s')->setwidth(140); //second page... $objphpexcel->setactivesheetindex(1); $activesheet1=$objphpexcel->getactivesheet(); $query="........"; $result=exec_query($query); $num_fields = mysql_num_fields($result); $x='a'; $columns=array(); $i=0; for($i=0;$i<$num_fields;$i++) { $columns[]=mysql_field_name($result, $i); $activesheet1->setcellvalue($x.'1', utf8_encode($columns[$i])); $x++; } $result=exec_query($query); $num_fields = mysql_num_fields($result); $x='a'; $k=2; while($row=fetch_array($result)) { for($i=0;$i<$num_fields;$i++) { $activesheet1->setcellvalue($x.$k, utf8_encode($row[$columns[$i]])); $x++; } $x='a'; $k++; } $activesheet1->settitle('other info'); $activesheet1->getcolumndimension('s')->setwidth(140); //end of sec page header('content-type: application/vnd.ms-excel'); header('content-disposition: attachment;filename="lexico_cms.xls"'); header('cache-control: max-age=0'); $objwriter = phpexcel_iofactory::createwriter($objphpexcel, 'excel5'); $objwriter->save('php://output');

the issue you're keeping big excel object in memory, , script runs out of usable memory.

first should seek determine if there ways cut down memory consumption of script, if fails need increment php memory limit. in php.ini file, locate memory_limit setting determines maximum amount of memory script may consume, e.g.

memory_limit = 64m;

increase value until no longer see error.

php phpexcel

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -