mysql_insert_id() vs LAST_INSERT_ID()

From: Date: Thu, 07 Feb 2002 15:08:06 +0000
Subject: mysql_insert_id() vs LAST_INSERT_ID()
Groups: php.general 
Request: Send a blank email to php-general+get-83844@lists.php.net to get a copy of this message
I have two questions: 1. Can anyone tell me whether the following statement is true or false? The PHP function mysql_insert_id() differs from the MySQL function LAST_INSERT_ID() in that the PHP function returns the last auto-incremented value from the current connection, and the MySQL function returns the highest current value in a table's AUTO_INCREMENT column. 2. Secondly, if anyone could answer the following question it would be very helpful... When updating a foreign key (in-between table for many-to-many relationships) using the last auto-incremented value, is it better to write several separate SQL queries and store them in the same variable for mysql_query(), or is it better to write several separate SQL queries and store them in separate variables, and then run separate mysql_query() functions against those variables separately? IOW, ex 1: $sql = "INSERT INTO table1 (column2, column3) VALUES ('value1', 'value2') INSERT IGNORE INTO table2 (column1, column2) VALUES (LAST_INSERT_ID(), '$id')"; $result = mysql_query($sql); OR, ex 2: $sql1 = "INSERT INTO table1 (column2, column3) VALUES ('value1', 'value2')"; $result1 = mysql_query($sql1); $last_inserted = mysql_insert_id(); $sql2 = "INSERT IGNORE INTO table2 (column1, column2) VALUES ('$last_inserted', '$id')"; $result2 = mysql_query($sql2); In other words, when performing multiple SQL queries simultaneously, is it better to give them their own variable (each SQL statement), or can you lump them all together? Thank you, Erik ---- Erik Price Web Developer Temp Media Lab, H.H. Brown pricee@hhbrown.com

« previous php.general (#83844) next »