note 81112 added to function.mysql-insert-id
| From: | venimus at gmail dot com | Date: | Thu, 14 Feb 2008 15:11:10 +0000 |
| Subject: | note 81112 added to function.mysql-insert-id | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-136146@lists.php.net to get a copy of this message | ||
Using 'SELECT MAX(id)+1...' will not return the next auto_increment id. This function is
totaly unreliable by two reasons.
1. In race conditions there is no guarantee that other user will not insert new record while your
function have done its work. This will render your "generated" last_id obsolete. It is
rare case but it happens.
2. Most of all, if the last record(s) in the table is deleted the max id will no longer match the
auto_increment value, because auto_increment never repeats numbers, it increases whenever an insert
statement is completed and does not decrease if you erase the last record!!!
e.g. If you have this table with the last record deleted:
id name
1. car
2. plane
3. truck - [erased]
auto_increment is 4
but MAX(id) is 2!!!
----
Server IP: 217.75.128.29
Probable Submitter: 87.126.199.164
----
Manual Page -- http://www.php.net/manual/en/function.mysql-insert-id.php
Edit -- https://master.php.net/note/edit/81112
Del: integrated -- https://master.php.net/note/delete/81112/integrated
Del: useless -- https://master.php.net/note/delete/81112/useless
Del: bad code -- https://master.php.net/note/delete/81112/bad+code
Del: spam -- https://master.php.net/note/delete/81112/spam
Del: non-english -- https://master.php.net/note/delete/81112/non-english
Del: in docs -- https://master.php.net/note/delete/81112/in+docs
Del: other reasons-- https://master.php.net/note/delete/81112
Reject -- https://master.php.net/note/reject/81112
Search -- https://master.php.net/manage/user-notes.php