Forum Moderators: coopster

Message Too Old, No Replies

mysql query using INSERT

         

Crump

1:17 am on Dec 2, 2006 (gmt 0)

10+ Year Member



Here is my situation:
I perform a INSERT statement in to a table. The table has a auto_increment column. So I do the INSERT, and it automatically creates a value in that column.

Now, is there a way to get that value? The only thing I can think of is after doing the INSERT, do a SELECT with some of the values that I used in the INSERT, but I am afraid of duplicate entries.

Any ideas?

grandpa

1:20 am on Dec 2, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



mysql_insert_id [php.net] should get it for you

mysql_insert_id -- Get the ID generated from the previous INSERT operation

coopster

1:00 am on Dec 3, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Although PHP supplies us with the mysql_insert_id() [php.net] function, we may be better of not using it. There is a warning in the manual regarding certain column types and a known issue, therefore I use the internal MySQL SQL function LAST_INSERT_ID() [dev.mysql.com] in an SQL query instead.