在PHP中过滤全部计数

时间:2012-09-27 05:01:31

标签: php mysql sql

如何在活动记录中转换此代码:

$last_points = mysql_insert_id(); 
$sql = "SELECT COUNT(fkmember) FROM downline WHERE fkmember = {$last_points}"; 

3 个答案:

答案 0 :(得分:4)

$this->db->select('COUNT(fkmember)');
$query = $this->db->get_where('downline', array('fkmember'=> $last_points))->num_rows();

答案 1 :(得分:2)

首先,当使用像COUNT这样的聚合函数时,建议使用GROUP BY

$last_points = $this->db->insert_id();

$this->db->from('downline');
$this->db->where('fkmember', $last_points);
$this->db->group_by('fkmember');
echo $this->db->count_all_results();
// Produce an integer, like 17

您也可以参考手册:http://codeigniter.com/user_guide/database/active_record.html

答案 2 :(得分:0)

$last_points = mysql_insert_id(); 

$count = $this->db->where('fkmember',$last_points)->get('downline')->num_rows();

$ count给出结果计数