Welcome Guest, Not a member yet? Register   Sign In
Database identity after an insert (probably a newbie question)
#1

[eluser]Antonio Fernández[/eluser]
Hi!

Usually when I was coding in raw php I was used to make something like this:

Code:
mysql_query("INSERT INTO `my_table` (a,b) VALUES ('aa','bb');
$id = mysql_insert_id();

getting the last id inserted on the table.

Are there something similar on CI?

Code:
$data = array(
'a' => 'aa',
'b' => 'bb'
);
$this->db->instert('mytable');

$id = mysql_insert_id();

This also works but I'm wondering if there is something like ...

Code:
$id = $this->db->insert_id();

... so you can be sure that you are getting the same id in the transaction.

Thanks a lot in advance.
#2

[eluser]Isern Palaus[/eluser]
[quote author="fernandezvara" date="1269979934"]This also works but I'm wondering if there is something like ...

Code:
$id = $this->db->insert_id();

... so you can be sure that you are getting the same id in the transaction.[/quote]

Hello fernandezvara,

You're right, $this->db->insert_id(); exists!

Check the userguide Query Helper Functions:

$this->db->insert_id()
The insert ID number when performing database inserts.

Regards,
Isern
#3

[eluser]Antonio Fernández[/eluser]
[quote author="Isern Palaus" date="1269980338"][quote author="fernandezvara" date="1269979934"]This also works but I'm wondering if there is something like ...

Code:
$id = $this->db->insert_id();

... so you can be sure that you are getting the same id in the transaction.[/quote]

Hello fernandezvara,

You're right, $this->db->insert_id(); exists!

Check the userguide Query Helper Functions:

$this->db->insert_id()
The insert ID number when performing database inserts.

Regards,
Isern[/quote]

Omg, I have tryed something like $this->db->insertid() :down:, and haven't found that option on the documentation.

Thanks a lot Isern.




Theme © iAndrew 2016 - Forum software by © MyBB