[PHP] mysql_insert_id() vs LAST_INSERT_ID()

2002-02-07 Thread Erik Price

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
[EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] mysql_insert_id() vs LAST_INSERT_ID()

2002-02-07 Thread Jason Wong

On Thursday 07 February 2002 23:08, Erik Price wrote:
 I have two questions:

I think the php-db list is more appropriate for these.

 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.

IIRC they both serve the same function. 

 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?


[snip]


 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?

I don't think MySQL is able to handle multiple queries in one statement. IOW 
you would have to perform each separately.


-- 
Jason Wong - Gremlins Associates - www.gremlins.com.hk

/*
When childhood dies, its corpses are called adults.
-- Brian Aldiss
*/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php