3

I am trying to get the last date for which I have the data. So I want to print the last date in my column date_data.

In Model:

public function last_record()
{ 
    $query = $this->db->select('LAST(date_data)');
    $this->db->from('date_data');
    return $query;
}

In Controller:

$last_data = $this->attendance_m->last_record();
var_dump($last_data);

But I am not getting the desired result

8 Answers 8

10

Try this

$query = $this->db->query("SELECT * FROM date_data ORDER BY id DESC LIMIT 1");
$result = $query->result_array();
return $result;
0
8

For getting last record from table use ORDER BY DESC along with LIMIT 1

return $last_row=$this->db->select('*')->order_by('id',"desc")->limit(1)->get('date_data')->row();
1
  • Thanks a lot for ur answer
    – Rajan
    Commented Jan 6, 2016 at 8:12
5

You can do this as below code:

public function get_last_record(){
     $this->db->select('*');
     $this->db->from('date_data');
     $this->db->order_by('created_at', 'DESC'); // 'created_at' is the column name of the date on which the record has stored in the database.
     return $this->db->get()->row();
}

You can put the limit as well if you want to.

2
public function last_record()
{ 

    return $this->db->select('date_data')->from('table_name')->limit(1)->order_by('date_data','DESC')->get()->row();
}    
2
  • Thanks a lot for ur answer
    – Rajan
    Commented Jan 6, 2016 at 8:12
  • yah, this answer is great! Thanks
    – Dylan B
    Commented Nov 12, 2017 at 5:23
2

Try this only with Active record

$this->db->limit(1);
$this->db->order_by('id','desc');
$query = $this->db->get('date_data');
return $query->result_array();
1

There is an easy way to do that, try:

public function last_record()
{ 
$query ="select * from date_data order by id DESC limit 1";
$res = $this->db->query($query);
return $res->result();
}
1
  • Thanks a lot for your answer
    – Rajan
    Commented Jan 6, 2016 at 8:12
0

Try this to get last five record:

/**
* This method is used to get recent five registration
*/
public function get_recent_five_registration(){
    return $this->db->select('name')
    ->from('tbl_student_registration')
    ->limit(5)
    ->order_by('reg_id','DESC')
    ->get()
    ->row();
}
0

$data = $this->db->select('*')->from('table')->where(id,$uid)->order_by('id',"desc")->limit(1)->get()->result();

Not the answer you're looking for? Browse other questions tagged or ask your own question.