codeigniter得到num_rows的数量

问题描述 投票:0回答:4

这是控制器

public function index()
{
    $data['page_title'] = 'Home';
    $data['new_grievances'] = $this->admin_home->get_members();
$data['num_row'] = count($data['new_grievances'] );
print_r($data['new_grievances']);
print_r($data['num_row']);
    $this->load->view('admin/grievances_admin_home' , $data);
}

这是我的模特

function get_members() {
    // $query = $this->db->get('enquiry');
    // return $query;
    $this->db->from('enquiry a');
    $this->db->join('enquiry_department b', 'a.department=b.id' , 'inner');
    $this->db->join('enquiry_user_type c', 'a.user_type=c.id' , 'inner');
    $this->db->WHERE('a.status IS NULL');
    $query = $this->db->get(); 
    return $query;
}

我想获得行数和行数。在这里,我执行mysql查询$ data ['new_grievances']行数为0.因此$ data ['num_row']的输出必须返回为零。但我有一个结果为1.然后我打印$ data ['new_grievances']值,它给出一个输出为

CI_DB_mysqli_result Object ( [conn_id] => mysqli Object ( [affected_rows] => 0 [client_info] => mysqlnd 5.0.11-dev - 20120503 - $Id: 76b08b24596e12d4553bd41fc93cccd5bac2fe7a $ [client_version] => 50011 [connect_errno] => 0 [connect_error] => [errno] => 0 [error] => [error_list] => Array ( ) [field_count] => 12 [host_info] ......

我不知道如何解决这个问题提出一些想法

php mysql codeigniter
4个回答
3
投票

将代码从return $query;更改为return $query->result();,您也可以在视图中使用$data并计算控制器中的总结果。

这是控制器

public function index()
{
    $data['page_title'] = 'Home';
    $data['new_grievances'] = $this->admin_home->get_members();
    $data['num_row'] = count($data['new_grievances'] );
    print_r($data['new_grievances']);
    print_r($data['num_row']);
    $this->load->view('admin/grievances_admin_home' , $data);
}

这是我的模特

function get_members() {
    $this->db->from('enquiry a');
    $this->db->join('enquiry_department b', 'a.department=b.id' , 'inner');
    $this->db->join('enquiry_user_type c', 'a.user_type=c.id' , 'inner');
    $this->db->WHERE('a.status IS NULL');
    $query = $this->db->get(); 
    return $query->result();
}

1
投票

请按照以下行。

public function index()
{
    $data['page_title'] = 'Home';
    $new_grievances = $this->admin_home->get_members();
    $data['num_row'] = $new_grievances->num_rows();
    print_r($new_grievances->result());
    $data['new_grievances'] = $new_grievances->result()
    print_r($data['num_row']);
    $this->load->view('admin/grievances_admin_home' , $data);
}

1
投票
public function index()
{
    $data['page_title'] = 'Home';
    $data['new_grievances'] = $this->admin_home->get_members();
    /*Old Code
    $data['num_row'] = count($data['new_grievances'] );*/
    //New Code
    $data['num_row'] = $data['new_grievances'];
    print_r($data['new_grievances']);
    print_r($data['num_row']);
    $this->load->view('admin/grievances_admin_home' , $data);
}

模型

function get_members() {
    $this->db->from('enquiry a');
    $this->db->join('enquiry_department b', 'a.department=b.id' , 'inner');
    $this->db->join('enquiry_user_type c', 'a.user_type=c.id' , 'inner');
    $this->db->WHERE('a.status IS NULL');
    return $this->db->count_all_results();
}

0
投票

只需使用以下行从DB获取结果

$ query = $ this-> db-> get(); $ results = $ query-> result();

并使用以下代码获取找到的行数$ count = $ query-> num_rows();

© www.soinside.com 2019 - 2024. All rights reserved.