RE: [PHP] auto increment
| From: | Chris Shiflett | Date: | Tue, 17 Feb 2004 20:03:41 +0000 |
| Subject: | RE: [PHP] auto increment | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-178017@lists.php.net to get a copy of this message | ||
--- "Chris W. Parker" <cparker@swatgear.com> wrote:
> > If the id is auto increment, can't you use max(id) to get the last
> > inserted one?
>
> only if you are the absolute only person that will ever be connected to
> that db at one time. otherwise the answer is no.
>
> what if in between your INSERT INTO statement and your SELECT statement
> someone else writes a record? for a high traffic website it's very
> possible that someone will insert another record before you can turn
> around and read the table.
This is a very good explanation. Also, before you start thinking that
these types of synchronization problems don't apply to you, because you're
on a dedicated box, consider that Apache (or whatever Web server you are
using) can potentially have many child processes serving all of your
users. Thus, there may be many connections to the database, and all of
them may be inserting into this table.
The function to retrieve the last ID is for a single connection, therefore
these types of synchronization issues aren't a problem.
Hope that helps.
Chris
=====
Chris Shiflett - http://shiflett.org/
PHP Security - O'Reilly
Coming mid-2004
HTTP Developer's Handbook - Sams
http://httphandbook.org/
PHP Community Site
http://phpcommunity.org/