CodeIgniter Forums
Fatal memory error - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Fatal memory error (/showthread.php?tid=74945)



Fatal memory error - manigopal - 11-27-2019

When i run this query for API. I am getting allowed memory size of 134217728 bytes exhausted.

Query is

public function categories_with_products(){

        $this->db->select('category_id,category_name,parent_category');
        $this->db->from('categories_info');
        $this->db->where('parent_category', 1);

        $parent = $this->db->get();
       
        $categories = $parent->result();
        $i=0;
        foreach($categories as $p_cat){

            $categories[$i]->sub_categories = $this->sub_categories_with_products($p_cat->category_id);
            $i++;
        }

        return $categories;

      /* echo json_encode (
                    array('cat'=>$categories,
                ));*/
    }

    public function sub_categories_with_products($id){

        $this->db->select('category_id,category_name,parent_category');
        $this->db->from('categories_info');
        $this->db->where('parent_category', $id);

        $child = $this->db->get();
        $categories = $child->result();
        $i=0;
        foreach($categories as $p_cat){

            $categories[$i]->sub_category = $this->sub_categories_products($p_cat->category_id);
            $i++;
        }
        return $categories;     

    }

    public function sub_categories_products($id){

        $this->db->select('product_name,category_id');
        $this->db->from('products_info');
        $this->db->where('category_id', $id);

        $child = $this->db->get();
        $categories = $child->result();
        $i=0;
        foreach($categories as $p_cat){

            $categories[$i]->products = $this->sub_categories_products($p_cat->category_id);
            $i++;
        }
        return $categories;     

    }


RE: Fatal memory error - InsiteFX - 11-28-2019

Try this to pinpoint the problem for us.

PHP Code:
// try this before your query to see if you need to increase
// your php.ini memory_limit

ini_set('memory_limit''1024M'); // or you could use 1G

// do a backtrace to see if there are other problems
// add this before your query to see if the problem is there

var_dump debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); 

Let us know what you find out.


RE: Fatal memory error - manigopal - 11-28-2019

i have used the memory increase at

AS $old = ini_set('memory_limit', '8192M');

at top of Model & controller page,

AND THIS where should i add this before which query ?
var_dump debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);

categories_info Table @ https://ibb.co/QDpykdD

products_info Table @ https://ibb.co/5cc3ZSM


RE: Fatal memory error - InsiteFX - 11-28-2019

First try setting the memory_limit, this can also be done in php.ini file.
try it before and after your queries to see if there is some other problem.