Danny Danny - 4 months ago 24
SQL Question

How to get URL ID into MySQL query for CodeIgniter

Basically, this is what I wanted to do, how does it work?

public function get_product_category(){


$id = $this->uri->segment(3);


$query = $this->db->query('SELECT * FROM products where category_id = $id ');


return $query->row();
}

Answer

You could use Query Builder Class

public function get_product_category(){

  $this->db->where('category_id', $this->uri->segment(3));
  $query = $this->db->get('products');

  return $query->row();
  // return $query->row_array();

}

Or

public function get_product_category(){

  $this->db->select('*');
  $this->db->from('products');
  $this->db->where('category_id', $this->uri->segment(3));
  $query = $this->db->get();

  return $query->row();
  // return $query->row_array();

}

I would autoload the database library.