|
Posted by newbie on 07/20/07 20:35
On Jul 20, 12:56 am, Rik <luiheidsgoe...@hotmail.com> wrote:
> On Fri, 20 Jul 2007 09:44:24 +0200, newbie <mitbb...@yahoo.com> wrote:
> > I am writing a DB like this:
> > ---------------------------------------------------------------------
> > | id (auto_incremented filed) | foo | bar |
> > ---------------------------------------------------------------------
>
> > $sql_query = "INSERT INTO `table` (`foo`, `bar`) VALUES ('$string_1',
> > '$string_2')";
> > mysql_query($sql_query, $db_handle);
> > //how can I get id for the above inserted entry? is that possible I
> > get it with a single mysql instruction?
>
> > 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.'. How to deal with the situation that multi-
users insert rows concurrently?
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.
It seems that by using the following code can't guarantee atomicity.
mysql_query("INSERT INTO `table` (`foo`, `bar`) values ('example foo',
'example bar')");
mysql_insert_id();
Thanks
> MySQL:
> mysql_query('SELECT LAST_INSERT_ID()');
>
> --
> Rik Wasmus
[Back to original message]
|