简体   繁体   中英

how to make this sql statment in codeigniter framework php?

how to make this sql statment in codeigniter framework php??

SELECT COUNT(`id`) AS `c` FROM `products` WHERE `valid` = 1 AND `sticky` = 2

how to make it in the model

like this way

$this->db->get('products');

how to do that?

 $this->db->select('count("id") as c');
$this->db->where('valid',1);
$this->db->where('sticky',2);
$result = $this->db->get('products');
   $this->db->select('COUNT(id) AS c');
   $this->db->from('products');
   $this->db->where('valid =', 1);
   $this->db->where('sticky =', 2);
   $query= $this->db->get();

I know this is a late answer, but personally when using the framework, I like to build statements as general purpose as possible. I absolutely love the ability to pass an array of conditions as the where in the query, plus I like using get_where, so... I'm submitting this because my answer's slightly different and I feel is more invasive into using the intricacies of the framework ;].

$where['valid'] = 1;
$where['sticky'] = 2;

$db->select('count("id") as c');
$query = $db->get_where('products',$where);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM