Codigniter查询返回错误计数 [英] Codigniter query returning wrong count

查看:85
本文介绍了Codigniter查询返回错误计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用代码点火器,并且正在构建查询以返回参加活动的人数.这样我就可以算出一个数字,并尝试计算出一个百分比.

I am using code igniter and I am building a query to return how many people attended an event. So I can take the number and try to work out a percentage.

我遇到的问题是我试图根据Codeigniter 3 Docs建立查询,但是由于某种原因我返回了错误的结果,根本无法弄清原因.

The issue I am having is I am trying to build the query according to Codeigniter 3 Docs, But I am returning the wrong result for some reason and simply cannot figure out why.

首先,这是我正在查询的数据库中的表:

First of all here is the table in my database I am querying:

这是我从控制器调用的函数:

Here is the function I am calling from my 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

I am selecting all from my attendance table, I am then stating I want all attendance from event with id 17 then I am also stating where attended is = 1

我想返回数字3,但是我要返回数字1.

I want to return the number 3 but I am returning number 1.

有人可以帮我看看我要去哪里了吗?

Can anyone help me see where I am going wrong please?

在@Vickel的感激帮助下,我得到了这项工作.这是返回正确结果的查询:

I got this working with the grateful help of @Vickel. Here is the query that returned the correct result:

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()

count() is a php built-in function, and in the way you use it, it "counts" the query string, therefore returns 1, no matter what

正确的CI方式是使用return $attendancecount->num_rows():请参见这里

the correct CI way is to use return $attendancecount->num_rows(): see here

这篇关于Codigniter查询返回错误计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆