致命错误:$ this-> db-> where() - > join() - > num_rows()

时间:2016-02-12 11:10:27

标签: php codeigniter pagination where mysql-num-rows

我想在dbign中进行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

Produk:

id_produk
kategori_id
kode_produk
status_produk

3 个答案:

答案 0 :(得分:1)

在调用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;
}

答案 1 :(得分:0)

您在这里缺少表名

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;
}

答案 2 :(得分:0)

您的查询应如下所示: -

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

OR

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