簡體   English   中英

如何在Codeigniter中獲取db查詢后的行數?

[英]How do I get the number of rows after a db query in Codeigniter?

我設法獲取返回的數據行以進行特定搜索,但我無法獲得已找到的行數。 這是我的代碼

我的控制器

function searchdata(){
    $result = $this->posts->search($_POST['searchterm']);
    $data['posts']= $result['rows'];
    $data['num_results']= $result['num_rows']; // num_rows is the number of rows returned from a search 

    $this-> load-> view('results_index',$data);
}

我的模特

function get_search($keyword)
{
$q = $this-> db-> select('*')-> from ('blog');
$q= $this->db->like('title',$keyword);
$q= $this->db->or_like('description',$keyword);

$results['rows']= $q-> get()-> result();
//$results['num_rows']= $q-> num_rows(); <--- this doesn't work, invalid method, commented out
return $results;

}

如果有幫助,這個https://www.codeigniter.com/user_guide/database/results.html可能會幫助您。 我以為會的

謝了,兄弟們。

問題是你試圖在非CI-DB對象上執行num_rows,你在數組上執行num_rows並且不存在,你需要在返回this-> db-> get的對象上調用num_rows ()。

我會改變它:

function get_search($keyword)
{
$this-> db-> select('*')-> from ('blog');
$this->db->like('title',$keyword);
$this->db->or_like('description',$keyword);

$q = $this->db->get();

$results['num_rows']= $q->num_rows(); 
$results['rows'] = $q->result();
return $results;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM