我正在使用代码点火器,并且正在构建一个查询来返回有多少人参加了事件。所以我可以拿这个数字并尝试计算出百分比。
我遇到的问题是我正在尝试根据 Codeigniter 3 文档构建查询,但由于某种原因我返回了错误的结果,并且根本无法弄清楚原因。
首先这是我正在查询的数据库中的表:
这是我从 Controller 调用的函数:
public function get_event_attendance_percentage($id) {
$this->db->select('*');
$this->db->from('attendance');
$this->db->where('event_id', $id );
$this->db->where('attended', 1 );
$attendancecount = $this->db->get();
return count($attendancecount);
}
我从我的出席表中选择所有内容,然后我声明我希望 ID 为 17 的事件的所有出席,然后我还声明出席的位置 = 1
我想返回数字 3,但我要返回数字 1。
谁能帮我看看我哪里出错了?
在@Vickel 的帮助下,我完成了这项工作。这是返回正确结果的查询:
public function get_event_attendance_percentage($id) {
$this->db->select('*');
$this->db->from('attendance');
$this->db->where('event_id', $id );
$this->db->where('attended', 1 );
$attendancecount = $this->db->get();
return $attendancecount->num_rows();
}
最佳答案
count()
是 php built-in function ,并且按照您使用它的方式,它会“计算”查询字符串,因此无论如何都会返回 1
正确的 CI 方法是使用 return $attendancecount->num_rows()
:请参阅 here
关于jquery - Codeigniter 查询返回错误计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54658052/