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

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  


 [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] User who submitted this comment has not confirmed identity
If you submitted this note, check your email.If you do not have a message, click here to re-send
MANUAL CONFIRMATION IS NOT POSSIBLE.  Write a message to pear-dev@lists.php.net
to request the confirmation link.  All bugs/comments/patches associated with this

email address will be deleted within 48 hours if the account request is not confirmed!
 [2004-05-06 12:43 UTC] User who submitted this comment has not confirmed identity
If you submitted this note, check your email.If you do not have a message, click here to re-send
MANUAL CONFIRMATION IS NOT POSSIBLE.  Write a message to pear-dev@lists.php.net
to request the confirmation link.  All bugs/comments/patches associated with this

email address will be deleted within 48 hours if the account request is not confirmed!
 [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] User who submitted this comment has not confirmed identity
If you submitted this note, check your email.If you do not have a message, click here to re-send
MANUAL CONFIRMATION IS NOT POSSIBLE.  Write a message to pear-dev@lists.php.net
to request the confirmation link.  All bugs/comments/patches associated with this

email address will be deleted within 48 hours if the account request is not confirmed!
 [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] User who submitted this comment has not confirmed identity
If you submitted this note, check your email.If you do not have a message, click here to re-send
MANUAL CONFIRMATION IS NOT POSSIBLE.  Write a message to pear-dev@lists.php.net
to request the confirmation link.  All bugs/comments/patches associated with this

email address will be deleted within 48 hours if the account request is not confirmed!
 [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] User who submitted this comment has not confirmed identity
If you submitted this note, check your email.If you do not have a message, click here to re-send
MANUAL CONFIRMATION IS NOT POSSIBLE.  Write a message to pear-dev@lists.php.net
to request the confirmation link.  All bugs/comments/patches associated with this

email address will be deleted within 48 hours if the account request is not confirmed!