Thread: Next Autoindex
View Single Post

   
  #3 (permalink)  
Old 02-28-2008, 09:27 AM
Peter H. Coffin
 
Posts: n/a
Default Re: Next Autoindex

On Wed, 21 Mar 2007 08:12:29 +0100, Axel Schwenke wrote:

>> 2. How do I obtain that value in PHP (as in, what would I use as the
>> index in the array returned by mysql_fetch_assoc())

>
> PHP gives you another possibility: use the mysql_insert_id() function:
> http://www.php.net/manual/en/functio...-insert-id.php
>
> The difference between the LAST_INSERT_ID() SQL function and the
> mysql_insert_id() API function is, that LAST_INSERT_ID() remembers
> the latest generated AUTO_INCREMENT value, even if there were
> other statements in between. OTOH mysql_insert_id() remembers the
> AUTO_INCREMENT value of the last statement. If you do some SQL
> statement that does not generate an AUTO_INCREMENT value, it will
> reset mysql_insert_id() to 0.
>
> Beware of the implicite connection sharing in PHP! If you
> use multiple database connections in the same PHP script,
> PHP may share connections (see here for an explanation)
> http://www.php.net/manual/en/function.mysql-connect.php
>
> This may spoil AUTO_INCREMENT values, even those from LAST_INSERT_ID.


IIRC, it's not impossible to make mysql_insert_id() all but
completely safe at the PHP interpreter level, and there was some
noise about doing so a few years back, so getting into the habit of
using mysql_insert_id() for preference over rolling your own with
LAST_INSERT_ID() might actually pay off someday.

--
For why should my freedom be judged by another's conscience?
-- Paul (I Corinthians 10:29)
Reply With Quote