Hi Son, On Monday, April 08, 2002 2:37 PM, Son Nguyen [SMTP:[EMAIL PROTECTED]] wrote: > > --- BD <[EMAIL PROTECTED]> wrote: > > Son, > > > > At 09:48 PM 4/7/2002, you wrote: > > >mysql> desc threads; > > > >+-------------+--------------+------+-----+---------+----------------+ > > >| Field | Type | Null | Key | Default | Extra > > | > > > >+-------------+--------------+------+-----+---------+----------------+ > > >| thread_ID | int(11) | | PRI | NULL | auto_increment > > | > > >| subject | varchar(255) | YES | | NULL | > > | > > >| author | varchar(30) | YES | | NULL | > > | > > >| last_by | varchar(30) | YES | | NULL | > > | > > >| views | int(11) | | | 0 | > > | > > >| date_posted | datetime | YES | | NULL | > > | > > > >+-------------+--------------+------+-----+---------+----------------+ > > >6 rows in set (0.00 sec) > > > > > > $SQL_insert_thread = "INSERT into threads "; > > > $SQL_insert_thread .= "(subject, author, last_by, views, "; > > > $SQL_insert_thread .= "date_posted) " values "; > > > $SQL_insert_thread .= "(\'some_subject\', \'test_username\', > > > $SQL_insert_thread .= "\'test_username\', 1, now())"; > > > > > > In the table threads above, I do have a field: "thread_ID" with > > >auto_increment value. My question is can I obtain the value > > thread_ID > > >while I do the insert statement just by 1 mySQL statement? If yes, > > >please can somebody give me a sample code? > > > > execute the MySQL query: select last_insert_id(); > > 1st: select last_insert_id() doesn't work > > 2nd: It's not the answer I am looking for ... Because of the race > condition if two users access the that script at the same time... I > will have when I leave the INSERT statement !!! > > Here is what I mean: think about this situation: > > USER 1 ===> INSERT > STOP <== due to process scheduling > > USER 2 ===> INSERT > SELECT last_insert_id(); > > USER 1 ===> SELECT last_insert_id(); > > It's the race condition, I am talking about. > True, this would seem to be the case, but MySQL returns the last_inert_id relative to the user and connection so as long as the user inserts and then selects in the same connection, it doesn't matter what other connections are doing because they are ignored. (I think if you did this in two different connections it would something meaningless anyway).
http://www.mysql.com/documentation/mysql/bychapter/manual_Reference.html #Reference - search for LAST_INSERT_ID - about halfway down the page. cheers, noel --------------------------------------------------------------------- Before posting, please check: http://www.mysql.com/manual.php (the manual) http://lists.mysql.com/ (the list archive) To request this thread, e-mail <[EMAIL PROTECTED]> To unsubscribe, e-mail <[EMAIL PROTECTED]> Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php