note 81112 deleted from function.mysql-insert-id by cmb

From: Date: Sun, 16 Feb 2020 13:44:38 +0000
Subject: note 81112 deleted from function.mysql-insert-id by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-214132@lists.php.net to get a copy of this message
Note Submitter: venimus at gmail dot com ---- 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!!!

« previous php.notes (#214132) next »