Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何计数Codeigniter中的每个用户并返回数组_Php_Database_Codeigniter - Fatal编程技术网

Php 如何计数Codeigniter中的每个用户并返回数组

Php 如何计数Codeigniter中的每个用户并返回数组,php,database,codeigniter,Php,Database,Codeigniter,我有这样的数据库,希望返回: id_user : 5c348f8041dc5 have 2 id_user : 5cfc8a7d33a12 have 1 如何做到这一点或我应该寻找的教程的名称 谢谢。您正在寻找一份计数和一份分组人声明 功能测试(){ $this->load->database(); $this->db->select('id_user,COUNT(*)as total'); $this->db->from('test');//用数据库表替换'test' $this->d

我有这样的数据库,希望返回:

id_user : 5c348f8041dc5 have 2  
id_user : 5cfc8a7d33a12 have 1
如何做到这一点或我应该寻找的教程的名称


谢谢。

您正在寻找一份
计数
和一份
分组人
声明

功能测试(){
$this->load->database();
$this->db->select('id_user,COUNT(*)as total');
$this->db->from('test');//用数据库表替换'test'
$this->db->group_by('id_user');
$q=$this->db->get();
如果($q->num_rows()==0){
显示_错误(“无行”);
}
foreach($q->result()作为$item){
回显“id\u用户:$item->id\u用户拥有$item->total
”; } }
结果:


什么是2和1?计数id\u用户的结果。
function test() {

    $this->load->database();

    $this->db->select('id_user, COUNT(*) as total');
    $this->db->from('test'); // replace 'test' with your database table
    $this->db->group_by('id_user');
    $q = $this->db->get();

    if ($q->num_rows() == 0) {
        show_error('no rows');
    }

    foreach ($q->result() as $item) {
        echo "id_user: $item->id_user have $item->total <br>";
    }

}