0

My need is write a data to multiple Excel files, every Excel file contains over 100 000 of data. I have used one php file to wrote a logic.

Here is my PHP code

$arr = array(2,4,5,6,7,10,11,12,13,14,15,16,17,18,20,21,26,27,28,29,31,33,34,35);

$age_arr = array("18"=>"age_0_4","1"=>"age_5_9","2"=>"age_10_14","3"=>"age_15_19","4"=>"age_20_24","5"=>"age_25_29","6"=>"age_30_34","7"=>"age_35_39","8"=>"age_40_44","9"=>"age_45_49","10"=>"age_50_54","11"=>"age_55_59","12"=>"age_60_64","13"=>"age_65_69","14"=>"age_70_74","15"=>"age_75_79","16"=>"age_80");

foreach($state_arr as $state_ind=>$state)
{


$fp = fopen("GenderAge-".$state.".csv","w");

$sql= "SELECT id_1,id_2,id_3,id_3,id_4 id_5,id_6,type,language,sum(".implode("),sum(",$age_arr)."),sum(age_0_4+age_5_9+age_10_14+age_15_19+age_20_24+age_25_29+age_30_34+age_35_39+age_40_44+age_45_49+age_50_54+age_55_59+age_60_64+age_65_69+age_70_74+age_75_79+age_80) total FROM db_name.table_name where stat!='R' group by id_1,id_2,id_3,id_4";

$qry_gen = mysql_query($sql_gen); 

while($res_age = mysql_fetch_array($qry_age))
{
    //fetch data and wrote some logic

    $str = "$id_1,$id_2,id_3,$id_4,$id_5,$id_6,$type,$language,$gen,".implode(",",$result)."\n";
    fwrite($fp,$str);
}

}

This php run perfectly and all csv file generated successfully in processor i4 and above . and doesn't work for i3 through fatal error of memory problem and maximum time execution. how to solve this problem if any easiest way to write csv file?

James Z
  • 12,209
  • 10
  • 24
  • 44
Balakumar B
  • 770
  • 5
  • 17
  • 41
  • I didn't put full php code instead of i put some of sample in above PHP code. – Balakumar B Jul 14 '16 at 05:26
  • this may be duplicate of http://stackoverflow.com/questions/561066/fatal-error-allowed-memory-size-of-134217728-bytes-exhausted-codeigniter-xml check it out, it may help you – Parag Chaure Jul 14 '16 at 05:32
  • add `fclose` after the `while` loop. Also, to write CSV, you can use the `fputcsv` function, instead of formatting the string yourself. – Adrien Jul 14 '16 at 14:11

0 Answers0