【问题标题】:How to get URL ID into MySQL query for CodeIgniter如何将 URL ID 获取到 CodeIgniter 的 MySQL 查询中
【发布时间】:2016-07-29 23:30:07
【问题描述】:
基本上,这就是我想做的,它是如何工作的?
public function get_product_category(){
$id = $this->uri->segment(3);
$query = $this->db->query('SELECT * FROM products where category_id = $id ');
return $query->row();
}
【问题讨论】:
标签:
mysql
codeigniter
codeigniter-3
【解决方案1】:
你可以使用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();
}
或者
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();
}
我会自动加载数据库库。