i have this next recursive function that keep return 500 error
public function get_parent($cat=0){
$ci = &get_instance();
$q= $ci->db->query("SELECT category_id,parent_id FROM tbl_products_categories WHERE category_id = {$cat}");
if($q->num_rows()>0){
$row = $q->row();
if($row->parent_id == 0){
return $row->category_id;
break;
}else if($row->parent_id != 0){
$this->get_parent($row->category_id) ;
}
}
else{
return 0;
}
}
i am calling the function like this:
$cat = 9;
$main_cat =$this->get_parent($cat);
$this->get_parent($row->category_id);
is calling get_parent with the same ID you originally called it with so it keep recursing until you get a stack overflow.
FYI Depending on your database, it should be possible to do what you want in pure SQL without multiple queries.
Edit: Also, you probably want to do
$ci->db->query("SELECT category ... WHERE category_id=?", array($cat));
instead of what you have now because your current query is susceptable to SQL injection if your function is called with anything other than a number.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.