retrieving new record ID? - 07-18-2004 , 01:50 PM
I'm making a web-site thats backended by php-2-mysql. A common problem seems
to be when processing form data, I use information early in the form to
create a new record in a main table. I then immediately need the
auto_incremented Key-ID of the new record to put other information in
In the past i've gotten that ID by just doing a SELECT with a WHERE that
matches all the values i've just INSERTED, but it seems a cumbersome
Is there a better or 'right-way' to have this ID returned to me?
Re: retrieving new record ID? - 07-18-2004 , 04:37 PM
But you propably want to use this:
"The value of mysql_insert_id() is affected only by statements issued
within the current client connection. It is not affected by statements
issued by other clients."
Which means, that if you have a php script like this:
It is safe to use mysql_insert_id(), even when there would be multiple
clients using the same script at the same time. Because client would get
the last id from her/his query, not from others queries.
But if you would use "select max(id) from table" or similar, you would
get wrong behavious if two clients would happen to be doing the same
thing at the same time.