2

在 CodeIgniter 中检索最后一行(按 id 排序)的正确方法是什么?我试过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'];  

在这里,我得到了第一个值,但我想要最后一行。

4

3 回答 3

9
$this->db->select('id')->order_by('id','desc')->limit(1)->get('table_name')->row('id');
于 2014-08-06T10:53:18.400 回答
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'];
于 2013-02-19T14:21:24.383 回答
0
$query = $this->db->query("SELECT * FROM pagination ORDER BY id DESC LIMIT 1")->row_array();

应该管用?

于 2013-02-19T14:10:03.160 回答