Re: Getting auto_increment's next value
| From: | Tom Woody | Date: | Thu, 15 May 2003 21:39:02 +0000 |
| Subject: | Re: Getting auto_increment's next value | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-147824@lists.php.net to get a copy of this message | ||
On Thu, 2003-05-15 at 15:42, Daniel Stark wrote:
> I'm wondering if there is a tutorial on a good way to handle adding new
> records to a mySQL database and getting back the primary key.
> Specifically are people:
>
> 1. inserting the record and then querying for the id based on the add
> values
>
> or
>
> 2. Getting the max(primary_key) and inserting the record with this
> value.
>
> When I search the archive on this subject I saw some examples of the
> second one but none accounted for locking the db to prevent primary key
> hijacking. The first method would seem to suffer in cases where
> identical data exists in the database.
If the table uses auto_increment primary key than after inserting the
data into the database, mysql_insert_id() will return the value of the
auto_incremented field. I gather the information that needs to be in
the database, insert it, than get the auto_incremented fields to be used
to populate the other tables that are related.
As for your #2 question, there is no way to prevent PK hijacking, so you
are asking for trouble...auto_increment fields put those tasks to the
dbms and it handles that for you.
If you would like to see some examples, you can email me offlist and I
will send them to you, as I deal with stuff like this on a regular
basis.
--
Tom Woody
In a world without boundaries why
do we need Gates and Windows?