如何在CodeIgniter中获取表的最后一条记录?

时间:2014-12-12 06:11:37

标签: codeigniter

如何在Codeigniter中获取表的最后一条记录?

我的表名是帖子我想获得此表中最后一个记录的最后一个id或下一个id!

7 个答案:

答案 0 :(得分:2)

只需按ID按降序排序表数据,然后选择最后一行插入的数据。实施例

$last_row=$this->db->select('id')->order_by('id',"desc")->limit(1)->get('post')->row();

$ last_row将使用最后一行初始化

答案 1 :(得分:2)

//recommended don't use * from getting rows, insted of (*) please mention column names.

$row = $this->db->select("*")->limit(1)->order_by('id',"DESC")->get("table name")->row();
echo $row->id; //it will provide latest or last record id.

答案 2 :(得分:1)

$row = $query->last_row();// To get last record form the table
echo $row->id; // To print id of last record

答案 3 :(得分:1)

$this->db->select("*");
$this->db->from("table name");
$this->db->limit(1);
$this->db->order_by('id',"DESC");
$query = $this->db->get();
$result = $query->result();

答案 4 :(得分:0)

试试这个:

$insert_id = $this->db->insert_id(); 

示例:

function add_post($post_data){
   $this->db->trans_start();
   $this->db->insert('posts',$post_data);
   $insert_id = $this->db->insert_id();
   $this->db->trans_complete();
   return  $insert_id;
}

答案 5 :(得分:0)

在sql查询中使用限制并获取最后一行。 像这样SELECT * FROM your_table ORDER BY your_auto_increment_field DESC LIMIT 1

答案 6 :(得分:0)

试试这个

$this->db->get('table_name')->order_by('id','desc')->limit(1);

希望这对你有用。