note 83678 deleted from function.mysql-insert-id by felipe
| From: | felipe@php.net | Date: | Sat, 07 Jun 2008 18:15:42 +0000 |
| Subject: | note 83678 deleted from function.mysql-insert-id by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-140206@lists.php.net to get a copy of this message | ||
Note Submitter: Parham
----
For the sake of clarification above with regard to transactions, let's say I have these three
tables: T1, T2, T3. T1 has an auto_increment column "auto", all other columns in T1, T2,
and T3 have static content.
With this code I always get unique numbers even if I have several browser windows open and hit my
PHP script all at the same time:
<?php
$result = mysql_query("BEGIN", $connection);
$result = mysql_query("INSERT INTO T1 (static1) VALUES ('test')", $connection);
$result = mysql_query("SELECT LAST_INSERT_ID()", $connection);
$insert_id = mysql_result($result, 0);
echo $insert_id;
sleep(15);
$result = mysql_query("INSERT INTO T2 (static1, static2) VALUES ($insert_id,
'test$insert_id')", $connection);
$result = mysql_query("INSERT INTO T3 (static1, static2) VALUES ($insert_id,
'test$insert_id')", $connection);
$result = mysql_query("COMMIT", $connection);
?>
So then my question is this. Is it true that even though I have not committed anything, a call to
LAST_INSERT_ID() will ALWAYS give me the last insert id, even of other connections that have not
committed yet. In other words, if I have 100 clients hitting the above script at the same time, all
of them calling LAST_INSERT_ID before any of them commit anything, they should all still get unique
IDs? That's what I'm seeing when I run my mini-tests, but I just want to clarify. I
haven't been able to find a whole lot of clear concrete information regarding this function and
transactions. If someone could clarify this point with regard to how IDs are distributed, and what
they mean when they say it's on a "per connection" basis, that would be great!