Fatal error: $this->db->where()->join()->num_rows()

Fatal error: $this->db->where()->join()->num_rows()

我想使用 where db 查询进行 codeigniter 分页。

我的模型: (更新)

public function get_all_produk_row($url = ''){
  data = array();
  $this->db->where(array('kategori.url'=>$url,'produk.status_produk'=>'1'));
  $this->db->order_by('kategori_id');
  $this->db->join('kategori','kategori.id_kategori=produk.kategori_id');
  $this->db->num_rows();
  return $data;
}

但是我遇到了致命错误:Call to undefined method CI_DB_mysql_driver::num_rows()

我该如何解决?


我的数据库

类别:

id_kategori
kode_kategori
url

产品:

id_produk
kategori_id
kode_produk
status_produk

您在这里缺少 table 姓名

public function get_all_produk_row($url = '')
{     
  $data = $this->db->get_where("kategori",array('kategori.url'=>$url,'produk.status_produk'=>'1'))->num_rows(); 
 //or one method can be if you can write query 
 //$data = $this->db->query("select count(*) as total tbl_name")->row()->total; 
  return $data;
}

您的查询应该如下:-

$this->db->select('*');
$this->db->from('table');
$this->db->where($your_conditions);
$num_results = $this->db->count_all_results();

$this->db->select('*');
$this->db->where('whatever');
$query = $this->db->get('table');
$num = $query->num_rows();

调用 get() 函数后您无法获取行数:

public function get_all_produk_row($url = '')
{ 
    $data = array(); 
    $this->db->where(array('kategori.url'=>$url,'produk.status_produk'=>'1')); 
    $this->db->join('kategori','kategori.id_kategori=produk.kategori_id'); 
    $this->db->order_by('kategori_id'); 
    $query = $this->db->get();

    if ($query->num_rows() > 0)
    {
         $data = $query->result_array();
    }
    return $data;
}