CodeIgniter Forums
Selecting the record I've just inserted? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Selecting the record I've just inserted? (/showthread.php?tid=7544)



Selecting the record I've just inserted? - El Forum - 04-14-2008

[eluser]KeyStroke[/eluser]
I'm using active records in CI to insert records.

In one table, the ID is a primary key set to auto-increment, so I just leave it empty while inserting and the database will assign an ID number for it.

However, I have to use that same ID to insert a record into a related table right after that.

My question is, how do I select the ID of the record I've just inserted?


Thanks in advance!


Selecting the record I've just inserted? - El Forum - 04-14-2008

[eluser]xwero[/eluser]
you can use $this->db->insert_id().


Selecting the record I've just inserted? - El Forum - 04-14-2008

[eluser]KeyStroke[/eluser]
Thanks xwero. Works perfectly now. Smile


Selecting the record I've just inserted? - El Forum - 04-14-2008

[eluser]Rubiz'[/eluser]
Pretty cool this information, I will use it in my next app Smile


Selecting the record I've just inserted? - El Forum - 06-18-2008

[eluser]BonnyBoboll[/eluser]
Hi !

I update this topic just to know if someone can put there a little piece of code with the function Insert_id() because I have to use it but I don't really know how ...

Thanks in advance =)


Selecting the record I've just inserted? - El Forum - 06-18-2008

[eluser]xwero[/eluser]
Code:
$this->db->set('field',$value)->insert();
return $this->db->insert_id();



Selecting the record I've just inserted? - El Forum - 06-18-2008

[eluser]BonnyBoboll[/eluser]
Thanks a lot !

It's totally what I was looking for !

Thank you !