Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.12.2

Request #1343 Last Insert Id
Submitted: 2004-05-05 00:52 UTC
From: jeff at luckettwebdev dot com Assigned: danielc
Status: Wont fix Package: DB
PHP Version: Irrelevant OS: any
Roadmaps: (Not assigned)    
Subscription  
Comments Add Comment Add patch


Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know! Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem : 15 - 13 = ?

 
 [2004-05-05 00:52 UTC] jeff at luckettwebdev dot com
Description: ------------ I would like a method that returns the ID of the last INSERT. Something similar in function to mysql_inser_id(); Thanks! Great package here, really enjoying my first implementation with it. -Jeff

Comments

 [2004-05-05 08:11 UTC] dufuz
I suppose you should use nextId() then ?
 [2004-05-06 12:43 UTC] danielc
You have a valid point. The difficulty is not all database systems have that capability. There have been discussions about a way to implement this on a cross platform basis, so it may come in the future. New features need to wait for the next new features release, so action won't be taken on this right away. Thanks.
 [2004-05-06 16:35 UTC] jeff at thetradeslist dot com
Thank you for taking the time to respond to me. So far, I have been having a very positive experience using DB, but I do miss having mqsql_insert_id(). Also, the fact that I am utilizing an existing database precludes me from using nextId() in any easily implementable fashion. I have a few ideas off the top of my head on how you might implement this cross-platform ... but I'm sure you've put a lot more thought into it than I. I understand that this feature would not be implemented until a future release, if at all! I'd love to see it added one day, but until that day comes, I'll just kludge my way around it. Thanks again! -Jeff
 [2004-06-10 22:19 UTC] thesaur
One thing you could try is adding the tables for nextId() manually and setting the initial value of the insert_id to whatever would come next for auto_increment. Then you can use nextId without modifying everything.
 [2005-09-25 04:26 UTC] pansunyou at gmail dot com
in the class defined by myself I use below code to insert data to mysql database,and it will return the id of new added data. ----------------------------- function insert($query) { $this->db->query($query); $result =& $this->db->query('select last_insert_id()'); list($lastInsertId) = $result->fetchRow(); if ($lastInsertId) { return $lastInsertId; } else return false; } // end func ---------------------------
 [2005-09-25 04:34 UTC] jeff at luckettwebdev dot com
pansunyou, I have taken to using an approach similar to this ... but this really isn't 'safe'. It'll probably be ok 99.9% of the time ... but there is the chance that another insert COULD happen between the time you make your insert, and you make the call to LAST_INSERT_ID(); Since in MySQL (at least), this call is not at all table specific ... it just happens to be the last auto-incremented value of any insert to any table ... there's a chance that you'll get the wrong insert ID for the record you just inserted.
 [2005-10-07 21:43 UTC] php dot devel at homelinkcs dot com
So long as you keep your script instances on seperate connections, LAST_INSERT_ID() is safe to use: "The last ID that was generated is maintained in the server on a per-connection basis. This means the value the function returns to a given client is the most recent AUTO_INCREMENT value generated by that client. The value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that you can retrieve your own ID without concern for the activity of other clients, and without the need for locks or transactions." - http://dev.mysql.com/doc/mysql/en/information-functions.html
 [2005-10-07 21:50 UTC] php dot devel at homelinkcs dot com
Futhermore, by all appearances, the PHP function mysql_insert_id() returns returns the same value as the MySQL function LAST_INSERT_ID(). So I think the only difference is in convenience and possibly speed.
 [2005-10-07 21:52 UTC] lsmith
The difference is also that mysql_insert_id() casts to a php integer, so the range of possible values is smaller than if you fetch LAST_INSERT_ID()
 [2006-02-27 10:49 UTC] dave at utwire dot net (dave)
Take a look at the code, if you are using mysql nextID calls mysql_insert_id(), and adds +1 to it. So all you have to do is call nextID(), and subtract 1 from the number... right?
 [2006-03-11 09:52 UTC] lsmith
DB is in maintenance mode and this feature is already implemented in MDB2.