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;
Sign up to request clarification or add additional context in comments.

Comments

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 Comment

Thanks a lot for ur answer
6

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.

Comments

2
public function last_record()
{ 

    return $this->db->select('date_data')->from('table_name')->limit(1)->order_by('date_data','DESC')->get()->row();
}    

1 Comment

Thanks a lot for ur answer
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();

Comments

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 Comment

Thanks a lot for your answer
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();
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.