在CodeIgniter中检索最后一行(按id排序desc)的正确方法是什么?
我试过select_max
,但我想要整行。
function getLastPagination(){
$data = array();
$this->db->query("SELECT id,sort FROM pagination GROUP BY id DESC LIMIT 1");
$query = $this->db->get('pagination');
$data = $query->row_array();
return $data;
}
$data['allpag'] = $this->Model_cats->getLastPagination();
$data['pagin'] = $data['allpag']['sort'];
$per_page = $data['pagin'];
这里我得到第一个值,但我想要最后一行。
答案 0 :(得分:10)
$this->db->select('id')->order_by('id','desc')->limit(1)->get('table_name')->row('id');
答案 1 :(得分:6)
试试这个.............
function getLastPagination(){
$query ="select * from pagination order by id DESC limit 1";
$res = $this->db->query($query);
if($res->num_rows() > 0) {
return $res->result("array");
}
return array();
}
}
在控制器功能中,你需要做以下事情.......
$allpag = $this->Model_cats->getLastPagination();
$per_page = $allpag[0]['sort'];
答案 2 :(得分:0)
$query = $this->db->query("SELECT * FROM pagination ORDER BY id DESC LIMIT 1")->row_array();
应该有用吗?