Reputation: 1599
Hi my table structure is given below
login_session_id, user_id, created_date, ci_cession_id, user_agent_string
The created_date field is mysql_date_time.
I want to get the latest row from this table (based on the created_date
field). How to do it with CI Active record?
Upvotes: 1
Views: 4620
Reputation: 885
the best way is
$this->db->select_max('date');
$query = $this->db->get('members'); // Produces: SELECT MAX(date) as date FROM members
return $query->result();
https://www.codeigniter.com/userguide3/database/query_builder.html
Upvotes: 1
Reputation: 2985
Try this:
$this->db->select('*');
$this->db->from('** YOUR TABLE HERE **');
$this->db->order_by('created_date', 'desc');
$this->db->limit(1);
$query = $this->db->get();
This should work by selecting all columns from the table (which you'll need to specify), ordering all rows with the most recent date at the top, then limiting it to the top row only which will be the most recent entry.
Upvotes: 7
Reputation: 2528
use order_by()
$this->db->select('login_session_id, user_id, created_date, ci_cession_id, user_agent_string');
$this->db->from("table_name");
$this->db->order_by("created_date", "desc");
$query = $this->db->get();
return $query->result();
this will do the trick
For more take a reference from HERE
Upvotes: 1