note 83678 added to function.mysql-insert-id
| From: | Parham at osu1 dot php dot net | Date: | Sat, 07 Jun 2008 06:05:08 +0000 |
| Subject: | note 83678 added to function.mysql-insert-id | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-140201@lists.php.net to get a copy of this message | ||
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!
----
Server IP: 66.207.199.35
Probable Submitter: 69.158.1.51
----
Manual Page -- http://www.php.net/manual/en/function.mysql-insert-id.php
Edit -- https://master.php.net/note/edit/83678
Del: integrated -- https://master.php.net/note/delete/83678/integrated
Del: useless -- https://master.php.net/note/delete/83678/useless
Del: bad code -- https://master.php.net/note/delete/83678/bad+code
Del: spam -- https://master.php.net/note/delete/83678/spam
Del: non-english -- https://master.php.net/note/delete/83678/non-english
Del: in docs -- https://master.php.net/note/delete/83678/in+docs
Del: other reasons-- https://master.php.net/note/delete/83678
Reject -- https://master.php.net/note/reject/83678
Search -- https://master.php.net/manage/user-notes.php