Page 1 of 1

Using mysql_insert_id

Posted: Fri Jan 11, 2008 12:01 am
by GeXus
Has anyone experienced problems when using mysql_insert_id? Such as the wrong id being grabbed? Perhaps on highly transactional sites? I haven't encountered anything, but I'm just wondering if it could pose a problem in the future

Re: Using mysql_insert_id

Posted: Fri Jan 11, 2008 12:12 am
by RobertGonzalez
There is a serious gotcha with mysql_insert_id as it relates to stored procedures/functions. Generally speaking the call to fetch the insert ID should be done immediately following the call that inserts. This usually alleviates clash, but not always.

If you can use a stored procedure, selecting the insert id immediately after the insert is a good bet that you will get what you are after.

Re: Using mysql_insert_id

Posted: Fri Jan 11, 2008 12:14 am
by GeXus
What if it's just all inline?

I.e.,

Code: Select all

mysql_query("insert....");
$id = mysql_insert_id();

Re: Using mysql_insert_id

Posted: Fri Jan 11, 2008 12:16 am
by RobertGonzalez
Right, that is what I am talking about... calling it immediately after the insert.

Re: Using mysql_insert_id

Posted: Fri Jan 11, 2008 12:30 am
by GeXus
Ok cool thnx!

Re: Using mysql_insert_id

Posted: Fri Jan 11, 2008 1:13 am
by Kieran Huggins
make sure to reference the DB handle, or it will assume the last transaction, whatever it was.