Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.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中获得db查询后的行数?_Php_Mysql_Codeigniter - Fatal编程技术网

Php 如何在Codeigniter中获得db查询后的行数?

Php 如何在Codeigniter中获得db查询后的行数?,php,mysql,codeigniter,Php,Mysql,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 row

我成功地获取了为执行特定搜索而返回的数据行,但无法获取已找到的行数。这是我的密码

我的控制器

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;

}
函数获取搜索($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();问题是,如果您试图在非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;

}

工作非常完美。当这个错误再次发生时,我会更加关心这些事情。谢谢!