EllisLab text mark
Advanced Search
     
getting last insert id
Posted: 10 May 2009 09:45 AM
Joined: 2009-05-10
4 posts

I am using $this->db->query() function to execute insert query(classical style).At this time if i am using $this->db->insert_id() function to retrive last insert id.But that function always returns 0 instead of last insert id.I am using mysql db.So please guide me or provide function to get last insert id with classical style.

 
Posted: 10 May 2009 10:49 AM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2008-05-18
2398 posts

You can use the MySQL function mysql_insert_id()

 Signature 

I’m building a Project Management System for my 3rd year Uni project, Sign up to the beta
Track my progress | Post of the day: UI Designs
Get full auto complete support for CodeIgniter in Eclipse

 
Posted: 11 May 2009 09:03 AM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2007-07-30
2144 posts

CodeIgniter also has the very helpful $this->db->insert_id();

 Signature 

Follow me on twitter here.
MichaelWales.com | MichaelWales.info

 
Posted: 11 May 2009 09:36 AM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2008-05-18
2398 posts

@Michael: Tut tut tut, didn’t read the thread properly

ganno - 10 May 2009 01:45 PM

At this time if i am using $this->db->insert_id() function to retrive last insert id.But that function always returns 0 instead of last insert id.

(Sorry, had to be said raspberry)

 Signature 

I’m building a Project Management System for my 3rd year Uni project, Sign up to the beta
Track my progress | Post of the day: UI Designs
Get full auto complete support for CodeIgniter in Eclipse

 
Posted: 11 May 2009 10:41 AM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2007-07-30
2144 posts

Whoops - nice catch! I am curious as to why the function is returning 0 everytime…

 Signature 

Follow me on twitter here.
MichaelWales.com | MichaelWales.info

 
Posted: 11 May 2009 10:53 AM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2008-05-18
2398 posts

Without ooking at its implementation, these are onlky guesses
- There is no auto incrementing ID
- Its a multipart primary key (but this function can only return a single value)
- There may be more then one unique fields in the table

@Ganno: Does this happen with all your tables, or just one? Whats the schema for your table?

 Signature 

I’m building a Project Management System for my 3rd year Uni project, Sign up to the beta
Track my progress | Post of the day: UI Designs
Get full auto complete support for CodeIgniter in Eclipse