Welcome Guest, Not a member yet? Register   Sign In
How to the fetch id of newly created entry
#1

[eluser]Unknown[/eluser]
Hi there. I am new to using CodeIgniter but am really enjoying it's features. Can anyone please tell me how I can fetch the id of a newly inserted database entry? I am basically looking for a return similar to what I would get if I used

Code:
mysql_lastid

I am currently inserting by doing:

Code:
$this->db->insert('tablename')

But haven't been able to figure out from the documentation how I get the id back.

Thanks!
Ivar
#2

[eluser]Developer13[/eluser]
http://ellislab.com/codeigniter/user-gui...lpers.html
#3

[eluser]pointer[/eluser]
Code:
$this->db->limit(1);
$this->db->from('table');
$this->db->order_by('id', 'desc');
$data['query']=$this->db->get();
or
Code:
$data['query']=$this->db->query("SELECT id FROM table ORDER BY id DESC LIMIT 1");
#4

[eluser]Developer13[/eluser]
pointer, that is the long and inefficient way to do it.

Just follow the link to the database helpers -- simply calling $this->db->insert_id() after performing an insert will find the inserted id.
#5

[eluser]manilodisan[/eluser]
Or:

Code:
$this->db->call_function ('insert_id');
#6

[eluser]ray73864[/eluser]
ooooh, sooo complicated in all the ways.

try this: $this->db->insert_id()

ahh, D13 already said that earlier, should have read first.
#7

[eluser]Colin Williams[/eluser]
Or:

Code:
$first = 'InsErt_';
$second = 'ID';
$call_func = 'call_function';
$id = $this->db->$call_func(strtolower($first . $second));




Theme © iAndrew 2016 - Forum software by © MyBB