1

I have made one script like below in CodeIgniter framework.

Which don't give me exact cause of below error. I have tried to resolve this error as my best experience.But still its not much clear about error.

I know there is possible solution is set below line at starting of function. But this is not much reliable solution as far i know.

ini_set("memory_limit", "-1");

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 85 bytes)

CODE :

function read_data(){
        $fp=fopen("/var/log/test/testdata.log","a+");
        $start_date = "2018-07-12 00:00:00";
        $end_date = "2018-07-31 23:59:59";
        $table = "july_calls";
        $table_prefix = 'custom_';
        $insert_table = 'july_cc_cps';
        $start_date_strtotime = strtotime($start_date);
        $end_date_strtotime = strtotime($end_date);
        $j=0;
        $old_date = strtotime(date("Y-m-d H:i:s",strtotime($start_date .' -1 day')));
        for($i= $start_date_strtotime;$i <= $end_date_strtotime;$i++){
        $table_name = $table_prefix.date("Y_m_d",$i);
        $date = date("Y-m-d",$i);
        $datetime = date("Y-m-d H:i:s",$i);
        if(date("d",$old_date) != date("d",$i)){ 
            $drop_table_query = "DROP TABLE IF EXISTS ".$table_name;
            //fwrite($fp,"Drop table query :".$drop_table_query."\n");
            $this->db->query($drop_table_query);
            $create_table_query ="create table ".$table_name."(
                id varchar(60) NOT NULL,
                userid int(11) NULL DEFAULT 0,
                trunk_id smallint(6) NOT NULL,
                callstart datetime NOT NULL default '0000-00-00 00:00:00',
                callend datetime NOT NULL default '0000-00-00 00:00:00',
                billseconds smallint(6) NOT NULL default 0
            ) ";
            fwrite($fp,"create table query :".$create_table_query."\n");
                    $this->db->query($create_table_query);
                    $insert_data_query = "insert into ".$table_name." select id,userid,trunk_id,callstart,callend,billseconds from ".$table." where callend >= '".$date." 00:00:00' AND callstart <='".$date." 23:59:59' and billseconds > 0 and trunk_id=10";
            //fwrite($fp,"insert data query :".$insert_data_query."\n");
                    $this->db->query($insert_data_query);
                    $before_yesterday_table = $table_prefix.date("Y_m_d",strtotime($datetime.' -2 day'));
                    $drop_old_table_query = "DROP TABLE IF EXISTS ".$before_yesterday_table;
                //fwrite($fp,"drop old table query :".$drop_old_table_query."\n");
                    $this->db->query($drop_old_table_query);
                    $drop_old_table_query = $drop_table_query= $create_table_query = null;
        }
        $insert_array =array();
        $query = "select count(uniqueid) as count,trunk_id from ".$table_name." where callstart <= '".$datetime."' AND callend >='".$datetime."' and trunk_id > 0 and billseconds > 0 and trunk_id=10 group by trunk_id";
        //fwrite($fp,"Trunk query :".$query."\n");
            $trunk_result = $this->db->query($query);
            if($trunk_result->num_rows() > 0 ){
                $trunk_result = $trunk_result ->result_array();
                    foreach($trunk_result as $key=>$value){
                        $insert_array[$i][$value['trunk_id']]['trunk_id'] = $value['trunk_id'];
                        $insert_array[$i][$value['trunk_id']]['asr']=0;
                        $insert_array[$i][$value['trunk_id']]['cc']=$value['count'];
                        $insert_array[$i][$value['trunk_id']]['cps']=0;
                        $insert_array[$i][$value['trunk_id']]['creation_date'] = date("Y-m-d H:i:s",$i);
                        $insert_array[$i][$value['trunk_id']]['userid']=0;
                    }           
            }
            $query = "select count(uniqueid) as count,trunk_id from ".$table_name." where callstart = '".$datetime."' and trunk_id > 0 and trunk_id=10 group by trunk_id";
                //fwrite($fp,"Trunk cps query :".$query."\n");
                $trunk_result = $this->db->query($query);
                if($trunk_result->num_rows() > 0 ){
                    $trunk_result = $trunk_result ->result_array();
                    foreach($trunk_result as $key=>$value){
                if(isset($insert_array[$i][$value['trunk_id']])){
                            $insert_array[$i][$value['trunk_id']]['cps']=$value['count'];
                }else{
                            $insert_array[$i][$value['trunk_id']]['trunk_id']=$value['trunk_id'];
                            $insert_array[$i][$value['trunk_id']]['asr']=0;
                            $insert_array[$i][$value['trunk_id']]['cc']=0;
                            $insert_array[$i][$value['trunk_id']]['cps']=$value['count'];
                            $insert_array[$i][$value['trunk_id']]['creation_date'] = date("Y-m-d H:i:s",$i);
                            $insert_array[$i][$value['trunk_id']]['userid']=0;
                }
                    }
                }
                if(!empty($insert_array)){
                    foreach($insert_array as $key=>$value){
                        foreach($value as $subkey=>$subvalue){
                            $this->db->insert($insert_table,$subvalue);
                        }
                    }
                }
                $old_date = $i;
                $query ="delete from ".$table_name." where callend <= '".$datetime."'";
                $this->db->query($query);
        }
        exit;
    }
Ankit Doshi
  • 1,164
  • 3
  • 21
  • 43
  • 1
    You should narrow down where the error happens. – Laurenz Albe Aug 20 '18 at 05:43
  • your double foreach over `$insert_array` is the problem - i guess you should insert data instantly because you are filling this array full with data which probably leads to memory troubles... – Atural Aug 20 '18 at 06:43

0 Answers0