RE: [PHP] best technique to get the ID of the last inserted value
| From: | Richard Lynch | Date: | Fri, 20 Jul 2007 21:18:09 +0000 |
| Subject: | RE: [PHP] best technique to get the ID of the last inserted value | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-259238@lists.php.net to get a copy of this message | ||
On Fri, July 20, 2007 2:07 pm, Uber Wannabe wrote:
> However, wouldn't obtaining the id in that manner still possibly lead
> to
> duplication? The DB would take care of the locking on the inserts,
> but the
> inserts would be populated from a non-locking select, right? Which
No.
In the case of Oracle (and others) the ID is GENERATED by that first
query:
$query = "select SEQUENCE_NAME.nextval";
$result = oracle_query($query);
$id = oracle_result($result, 0, 0);
//this $id is the ID we will use. It is unique.
You then have the guaranteed unique ID to pass in for your INSERT:
$query = "INSERT INTO whatever (id) VALUES ($id)";
There is no auto_increment on the id field, or it is a pointless
auto_increment, as the $id is passed in from the get-go.
> Just my thought... I'd stick with the DB's built-in functionality for
> that.
> As usual, all the information on that is freely available online.
The use of a "sequence" is a documented feature in many databases,
notably absent in MySQL.
Last I checked...
MySQL may have added "sequence" while I wasn't watching...
The databases with sequences also generally provide a function to
return the last-generated new ID on a per connection basis, as well,
which is much more similar to MySQL way.
--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?