|
Posted by Gordon Burditt on 07/20/07 22:45
>> > since each time I write a record into mysql, 'id' is incremented,
>> > how can I get the id that an insertion corresponds to (can I get it
>> > with a single mysql instruction)?
>>
>> PHP:
>> mysql_insert_id();
>
>Thanks for the answer. But I undersatnd mysql_insert_id() is to
>'Retrieves the ID generated for an AUTO_INCREMENT column by the
>previous INSERT query.'.
.... by the privious INSERT query *ON THIS CONNECTION*.
>How to deal with the situation that multi-
>users insert rows concurrently?
Don't deal with it: other users can (maliciously or not) insert
on other connections until they are blue in the face and not change
the result you get from mysql_insert_id().
>Say, I have a user_insert.php page, and each user accessing this page
>will insert a foo bar(say, both are user specified strings) a row, and
>he/she get an ID back---and he/she need to know exactly which ID his/
>her string corresponds to.
If you *want* interference between users, you'll need to do something
like "SELECT max(id) FROM ... "
>It seems that by using the following code can't guarantee atomicity.
As long as no other queries are made between the two statements
listed *ON THIS CONNECTION*, you're OK.
>mysql_query("INSERT INTO `table` (`foo`, `bar`) values ('example foo',
>'example bar')");
>mysql_insert_id();
Navigation:
[Reply to this message]
|