As of MySQL:
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id
The ID that was generated is maintained in the server on a
per-connection basis. This means that the value returned by the
function to a given client is the first AUTO_INCREMENT value generated
for most recent statement affecting an AUTO_INCREMENT column by that
client. This value cannot be affected by other clients, even if they
generate AUTO_INCREMENT values of their own. This behavior ensures
that each client can retrieve its own ID without concern for the
activity of other clients, and without the need for locks or
transactions.
Let's implement some testing:
<?php
header('Content-Type: text/plain');
$con1 = new mysqli('localhost', 'root', '1', 'new_schema');
$con2 = new mysqli('localhost', 'root', '1', 'new_schema');
$sql = 'DROP TABLE IF EXISTS `x`';
$con1->query($sql);
$sql = <<<SQL
CREATE TABLE `x`(
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`value` VARCHAR(20)
)ENGINE=InnoDB;
SQL;
$con1->query($sql);
$sql = <<<SQL
INSERT INTO `x` (`id`, `value`)
VALUES (NULL, NULL);
SQL;
$con1->query($sql);
$con2->query($sql);
echo 'con1 == con2 is ', $con1 == $con2 ? 'true' : 'false', PHP_EOL;
echo 'con1 === con2 is ', $con1 === $con2 ? 'true' : 'false', PHP_EOL;
echo '1: ', $con1->insert_id, PHP_EOL;
echo '2: ', $con2->insert_id, PHP_EOL;
$con1->close();
$con2->close();
?>
Shows:
con1 == con2 is true
con1 === con2 is false
1: 1
2: 2
Test subjects:
PHP 5.4.5
, MySQL 5.6.10
.