PDA

View Full Version : vUsing LAST_INSERT_ID, mysql_insert_id() to create foreign keys


in2mobi
09-22-2009, 06:59 PM
What I'm trying to resolve is which one of these functions I should use to get the last Inserted tbl id and also, how does mysql handle getting the last insert id. For instance, if I run an insert statement on table 1 and then run an insert statement on table 2 but would like to use the last_insert_id from table 1 do I have to reference table 1 in a special way, because it would seem to me that last_insert_id would want to grab the value from table 2 as opposed to table one. Any help on this topic is greatly appreciated.

Thank You

Old Pedant
09-22-2009, 08:10 PM
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id

The currently executing statement does not affect the value of LAST_INSERT_ID().

mysql_insert_id() is just the C API function for the same thing. I wouldn't use it if you can avoid doing so. Use the MySQL SQL syntax, instead.