154

Are there any options to get the last insert id of a new record in CodeIgniter?

$last_id = $this->db->insert('tablename',
    array('firstcolumn' => 'value',
    'secondcolumn' => 'value')
);

Considering the table consits of fields id (autoincrement) firstcolumn and secondcolumn.

This way you can use the insert id in the following code.

Pattle
  • 5,983
  • 8
  • 33
  • 56
DDecoene
  • 7,184
  • 7
  • 30
  • 43

11 Answers11

278

Shame on me...

I looked at the user guide and the first function is $this->db->insert_id();

This also works with activerecord inserts...

EDIT: I updated the link

Nafiu Lawal
  • 447
  • 1
  • 7
  • 16
DDecoene
  • 7,184
  • 7
  • 30
  • 43
  • 29
    the userguide is your best friend for things like this. i have been using codeigniter for 2+ years and i still find functions like this that i never knew about. – Tom Schlick Jan 03 '10 at 10:02
  • 8
    Be sure to wrap your insert and this function in a transaction as the query scheduler may insert another object before running this function – parker.sikand Dec 04 '13 at 18:28
  • 3
    link is dead, updated link: http://ellislab.com/codeigniter/user-guide/database/helpers.html – womd Feb 11 '14 at 11:16
  • I need to use insert_id(), but i don't know that insert_id() granted to get ID belong to insert() before it! I mean, Is it possible to retrieve ID belong another insert() in another request? – Majid Jan 03 '17 at 06:07
37

Last insert id means you can get inserted auto increment id by using this method in active record,

$this->db->insert_id() 
// it can be return insert id it is 
// similar to the mysql_insert_id in core PHP

You can refer this link you can find some more stuff.

Information from executing a query

BackSlash
  • 21,927
  • 22
  • 96
  • 136
Raja Ram T
  • 854
  • 8
  • 8
11

for Specific table you cannot use $this->db->insert_id() . even the last insert happened long ago it can be fetched like this. may be wrong. but working well for me

     $this->db->select_max('{primary key}');
     $result= $this->db->get('{table}')->row_array();
     echo $result['{primary key}'];
David
  • 1,147
  • 4
  • 17
  • 29
Infant Rosario
  • 149
  • 1
  • 4
8

List of details which helps in requesting id and queries are

For fetching Last inserted Id :This will fetching the last records from the table

$this->db->insert_id(); 

Fetching SQL query add this after modal request

$this->db->last_query()
Madhur
  • 1,702
  • 4
  • 23
  • 39
7

After your insert query, use this command $this->db->insert_id(); to return the last inserted id.

For example:

$this->db->insert('Your_tablename', $your_data);

$last_id =  $this->db->insert_id();

echo $last_id // assume that the last id from the table is 1, after the insert query this value will be 2.
ndrwnaguib
  • 5,623
  • 3
  • 28
  • 51
6

Try this.

public function insert_data_function($your_data)
{
    $this->db->insert("your_table",$your_data);
    $last_id = $this->db->insert_id();
    return $last_id;
}
Raham
  • 4,781
  • 3
  • 24
  • 27
  • @Dennis Decoene you may also visit this link https://ellislab.com/codeIgniter/user-guide. – Raham Feb 24 '15 at 09:42
6

$this->db->insert_id();

Try this As your desired question.

Akash Tyagi
  • 59
  • 1
  • 1
3
$this->db->insert_id();

Returns the insert ID number when performing database inserts

Query Helper Methods for codeigniter-3

Shaiful Islam
  • 7,034
  • 12
  • 38
  • 58
2
$this->db->insert_id()  

//please try this

Girish Ninama
  • 583
  • 4
  • 8
1
if($this->db->insert('Your_tablename', $your_data)) {
    return $this->db->insert_id();
}
return false 
0

In codeigniter 3, you can do it as follow:

if($this->db->insert("table_name",$table_data)) {
    $inserted_id = $this->db->insert_id();
    return $inserted_id;
}

You may get help for Codeigniter 3 from here https://codeigniter.com/user_guide/database/helpers.html

In Codeigniter 4, you can get last inserted id as follow:

$builder = $db->table("your table name");
if($builder->insert($table_data)) {
    $inserted_id = $db->insertID();
    return $inserted_id;
}

You may get help for Codeigniter 4 from here https://codeigniter4.github.io/userguide/database/helpers.html

Towsif
  • 337
  • 2
  • 12