如何获得表中的最后一个记录笨?(How to get last record of table in

2019-10-21 09:46发布

如何获得表中的最后一个记录笨?

我的表名是后我想最后一个记录的最后一个ID或一张ID在该表!

Answer 1:

只要您的表数据按ID进行降序排列,并挑选这将是最后插入数据的第一行。 例

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

$ LAST_ROW将与最后一排被初始化



Answer 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.


Answer 3:

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


Answer 4:

$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();


Answer 5:

尝试这个:

$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;
}


Answer 6:

使用限制在SQL查询,并得到最后一行。 类似这样的SELECT * FROM your_table ORDER BY your_auto_increment_field DESC LIMIT 1



Answer 7:

public function select(){
    $this->db->select("*");
    $this->db->from("post");
    $this->db->order_by('id',"DESC"); //You can use limit before it. if u want last 3 or 4 entries from database.. 
    $query = $this->db->get(); 
    if($query->num_rows() > 0){
        $category['rows'] = $query->num_rows();
        return $query->result();

    }
    else {
        return $query->result();

    }
    }// you can fetch your result in controller 


文章来源: How to get last record of table in CodeIgniter?
标签: codeigniter