$ this-> db-> insert_id () does not work in mysql codeigniter

I'm just trying to get the last automatically generated key from the mysql table from codeiginitor. But this does not work in any way. I tried this code
$this->db->insert_id() as well
$this->db->mysql_insert_id()
I get the following error
Severity: Notice
Message: Undefined property: CI_DB_mysql_driver::$insert_id

insert_id function not supported for mysql? How can we make it work?

+5
mysql codeigniter
source share
9 answers

This is the code I tried to insert the data and get the insert identifier,

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

This does not work for me. I used the DB driver as "mysql". Then I installed "mysqli" and changed the name of the DB driver in the database.php file, for example,
$db['default']['dbdriver'] = 'mysqli';
Now it works great for me.

+5
source share

CodeIgniter insert_id() returns only the identifier insert() . If you are not doing something like $this->db->insert('table', $data); before calling the function, it will not be able to return the identifier.

+11
source share

Use only this syntax

 $last_id = $this->db->insert_id(); return $last_id; 
+4
source share

Please check to see if your table has auto-increment, the primary field, and then $this->db->insert_id(); will not return anything.

+2
source share

assuming you use an active write template this should work $this->db->insert_id()

+1
source share

I had the same problem and I found a problem with this link: http://kedyr.wordpress.com/2012/10/03/codeigniter-insert_id/

The solution is not a proper solution because it is just a way to get around using native sql using the codeigniter "Query" method and processing the sql callback.

  $this->db->insert('references', $data); $query = $this->db->query('SELECT LAST_INSERT_ID()'); $row = $query->row_array(); $LastIdInserted = $row['LAST_INSERT_ID()']; 
+1
source share

try the following:

 $id = $this->db->insert_id() 

instead

 $id = $this->db->mysql_insert_id(); 
0
source share

try it

 $this->db->insert('table_name',$data); $id = $this->db->mysql_insert_id(); 
0
source share

Your code will work if you have this code:

 var $table = 'your table name' $this->db->insert($this->table,$data1); return $this->db->insert_id(); 

CodeIgniter insert_id () will return only the insert identifier (), as mentioned earlier, and you will not forget the DB driver name settings in the database.php file, for example

$db['default']['dbdriver'] = 'mysqli';

0
source share

All Articles