note 81129 added to function.mysql-insert-id
| From: | venimusatgmaildotcom at osu1 dot php dot net | Date: | Thu, 14 Feb 2008 23:27:38 +0000 |
| Subject: | note 81129 added to function.mysql-insert-id | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-136164@lists.php.net to get a copy of this message | ||
To get the most accurate NEXT auto_increment id use this function
<?php
function mysql_next_id($table) {
$result = mysql_query('SHOW TABLE STATUS LIKE "'.$table.'"');
$rows = mysql_fetch_assoc($result);
return $rows['auto_increment'];
}
?>
Any other methods of predicting the next autogenerated id will fail in certain cases.
Most of the programmers try to get the MAX(id)+1 (or sort the table backwards and get the first id)
but this will cease to work when you delete the last record.
However in a race condition even this function may fail if other users insert records in the time
between calling this function and using the return value. You may use LOCK TABLES to prevent such a
case.
Anyway, the best advice I can give you is to RECONSIDER YOUR LOGIC TO AVOID USING AUTO_INCREMENT.
----
Server IP: 89.190.194.18
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/81129
Del: integrated -- https://master.php.net/note/delete/81129/integrated
Del: useless -- https://master.php.net/note/delete/81129/useless
Del: bad code -- https://master.php.net/note/delete/81129/bad+code
Del: spam -- https://master.php.net/note/delete/81129/spam
Del: non-english -- https://master.php.net/note/delete/81129/non-english
Del: in docs -- https://master.php.net/note/delete/81129/in+docs
Del: other reasons-- https://master.php.net/note/delete/81129
Reject -- https://master.php.net/note/reject/81129
Search -- https://master.php.net/manage/user-notes.php