Welcome Guest, Not a member yet? Register   Sign In
Last ID in db
#1

[eluser]_Smilie_[/eluser]
Hi,
Is there anyway that I can use active records to retrieve the last ID in my database?
#2

[eluser]Stefan Hueg[/eluser]
Code:
$this->db->select_max('ID');
$query = $this->db->get('my_table');
// Produces: SELECT MAX(ID) as ID FROM my_table
#3

[eluser]PhilTem[/eluser]
If you want to receive the last inserted ID (after an insert-query) you may also use

Code:
$last_id = $this->db->last_insert_id();

However, @Stefan's code works well for auto-incrementing tables. If you tend to use your own primary-key, you'd have to have a field which annotates the creation date or its time stamp by which you would sort DESC and limit it to (1,1) while selecting the primary-key field.




Theme © iAndrew 2016 - Forum software by © MyBB