Ananth Duari
Ananth Duari

Reputation: 2879

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

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?

Upvotes: 5

Views: 86073

Answers (12)

vijayabalan
vijayabalan

Reputation: 99

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

Upvotes: 0

korvus
korvus

Reputation: 334

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()'];

Upvotes: 2

Rudra
Rudra

Reputation: 555

// Store data into array
$data=array('name'=>'Rudra','email'=>'[email protected]');

//Insert Data Query
$this->db->insert('tableName',$data);

// Storing id into varialbe
$id = $this->db->insert_id();
return $id;

Upvotes: 1

Amol kamble
Amol kamble

Reputation: 1

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; 

Upvotes: -2

Nugroho Setiawan
Nugroho Setiawan

Reputation: 51

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';

Upvotes: 0

Faizan Khattak
Faizan Khattak

Reputation: 882

Try this

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

Upvotes: 0

tanveer javaid
tanveer javaid

Reputation: 131

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

Upvotes: 3

user3337174
user3337174

Reputation: 149

Use only this syntax

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

return $last_id; 

Upvotes: 6

RAJEEV
RAJEEV

Reputation: 11

try this:

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

instead of

$id = $this->db->mysql_insert_id();

Upvotes: 1

user1542
user1542

Reputation: 49

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

Upvotes: 1

Ananth Duari
Ananth Duari

Reputation: 2879

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.

Upvotes: 5

Chris Schmitz
Chris Schmitz

Reputation: 8247

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.

Upvotes: 12

Related Questions