[EMAIL PROTECTED] wrote:

Hello

Not sure if you thought of it or if it even applies to you but when you use
PEAR::DB::getOnce("SELECT LAST_INSERT_ID()"); to retrieve the id do consider concurrent access to the database when using it for inserting stuff. Been there and its a hassle to resolve this little huge bug ; )
Consider the following situation:


Two users ( A and B) make simultaniouse access to your server. A queries the Database for an id
B queries the Database for an id
A writes stuff into the database using the retrieved id.
Now A and B both have the same id and if B wants to insert something into the db using id it will fail if id has to be unique (like in a primary key, foreign key, etc...)
B has to querie the database for the correct id again.


So make sure you read lock the table for the query and the insert or make sure you never use the retrieved id for inserting stuff.

You are very confused. User A and User B are on different connections and LAST_INSERT_ID() is connection specific _and_ you only use it _after_ you make an INSERT. It will return the last auto_increment generated number on a _per connection_ basis.


So User A INSERTS, User B INSERTS, then they each call LAST_INSERT_ID(), they'll get their own number from their own INSERT. You then use the number in subsequent INSERTs to maintain relationships.

--
---John Holmes...

Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/

php|architect: The Magazine for PHP Professionals – www.phparch.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to