Welcome Guest, Not a member yet? Register   Sign In
Primary Key question
#1

[eluser]codelearn[/eluser]
Hi guys,

Is there a way to use the "insert" function of active record, and then retrieve the "id" column immediately following creation.

Example:

$this->db->set('tagline',$this->input->post('tagline'));
$this->db->insert('group_group');

Now I want the ID of the row I just created... without using the tagline/where statement.

Thanks!
#2

[eluser]davidbehler[/eluser]
http://ellislab.com/codeigniter/user-gui...lpers.html

The function your are looking for is this:
Code:
$this->db->insert_id()
The insert ID number when performing database inserts.
#3

[eluser]codelearn[/eluser]
So would I do:

$this->db->insert_id(‘group_group’);

Thanks
#4

[eluser]pistolPete[/eluser]
No, just
Code:
$this->db->insert_id()
without any arguments!
#5

[eluser]davidbehler[/eluser]
No need for the parameter.
Just to do this immediatly after your insert:
Code:
$primary_key = $this->db->insert_id();

That's it. Of course you can call your variable whatever you want.




Theme © iAndrew 2016 - Forum software by © MyBB