I am just trying to get the latest auto generated key from the mysql table from codeiginitor. But it is not working some how. I have tried this code
$this->db->insert_id()
as well as
$this->db->mysql_insert_id()
I am getting the following error
Severity: Notice
Message: Undefined property: CI_DB_mysql_driver::$insert_id
is the insert_id function not supported for mysql? How can we make it work?

- 2,859
- 11
- 35
- 42
-
Does the normal [mysql_insert_id()](http://ro.php.net/manual/de/function.mysql-insert-id.php) work for you? – sqwk Aug 04 '11 at 13:35
-
1It would help to see where you are running the query before this. – Chris Schmitz Aug 04 '11 at 14:11
-
@sqwk I am able to get Insert id on normal PHP only after installing MySQLi. I assume mysql_insert_id will work only with MySQLi. I wonder how can we make CodeIgnitor to make use of MySQLi – Ananth Duari Aug 04 '11 at 14:50
-
@Chris I have run the query in mysql command line client as well as PHP program using MYSQLi. But it is not working with CodeIgnitor connection. – Ananth Duari Aug 04 '11 at 14:51
-
No, let us see all your code, where you're doing the query in codeigniter and then trying to retrieve the insert ID – jondavidjohn Aug 04 '11 at 14:55
-
Post your insert code, $this->db->insert_id() will only work if you are using the $this->db->insert method. Until you post your code nobody can help you! – mattumotu Aug 05 '11 at 09:53
12 Answers
CodeIgniter's insert_id()
will only return an ID of an insert()
. Unless you are executing something like $this->db->insert('table', $data);
before calling the function it will not be able to return an ID.

- 8,097
- 6
- 31
- 41
-
1I got it working with the following changes.
1. I changed the dbdriver in config.php from mysql to mysqli
2. using the following code
$this->db->mysql_insert_id();
– Ananth Duari Aug 04 '11 at 15:01 -
obviously the insertion id we are talking is after insert some values using insert statement. – Ananth Duari Aug 04 '11 at 15:02
-
You never posted the code so I wasn't sure if you were using an Active Record `insert()`. – Chris Schmitz Aug 04 '11 at 15:59
Use only this syntax
$last_id = $this->db->insert_id();
return $last_id;

- 22,443
- 7
- 33
- 51

- 149
- 1
- 7
This is the code I tried to insert the data and get the insert id,
$this->db->insert('table_name',$data);
$id = $this->db->mysql_insert_id();
It is not working for me. I have used DB Driver as "mysql". Then I have installed "mysqli" and changed the DB driver name in database.php like,
$db['default']['dbdriver'] = 'mysqli';
Now it is working fine for me.

- 2,859
- 11
- 35
- 42
I had the same problem, and found a solution with this link: http://kedyr.wordpress.com/2012/10/03/codeigniter-insert_id/
The solution is not properly a solution, because it's just a way to get around by using the native sql, with the codeigniter method "Query" and dealing with the native sql callback.
$this->db->insert('references', $data);
$query = $this->db->query('SELECT LAST_INSERT_ID()');
$row = $query->row_array();
$LastIdInserted = $row['LAST_INSERT_ID()'];

- 334
- 4
- 5
Please check if your table don't have auto increment, primary field then $this->db->insert_id();
will not return anything.

- 121
- 8
assuming you are using active record pattern
this should work
$this->db->insert_id()

- 21,208
- 8
- 66
- 122

- 49
- 1
- 9
// Store data into array
$data=array('name'=>'Rudra','email'=>'test@gmail.com');
//Insert Data Query
$this->db->insert('tableName',$data);
// Storing id into varialbe
$id = $this->db->insert_id();
return $id;

- 535
- 4
- 16
Try this
$this->db->insert('table_name',$data);
$id = $this->db->mysql_insert_id();

- 862
- 8
- 22
Your code will work if you have a code like this :
var $table = 'your table name'
$this->db->insert($this->table,$data1);
return $this->db->insert_id();
CodeIgniter's insert_id() will only return an ID of an insert(), as mention before, and also you dont forget to settings the DB driver name in database.php like
$db['default']['dbdriver'] = 'mysqli';

- 51
- 4
// insert data
$data=array("name"=>"vijay");
$this->db->insert("table_name",$data);
$insert_id= $this->db->insert_id();

- 91
- 1
- 3
If you are using ajax to insert data, then print the output you will get the id in response, or if you are using core codeigniter method then use below code.
$last_id = $this->db->insert_id();
return $last_id;

- 3,674
- 4
- 18
- 33

- 1
- 1