How to get next auto increment value of a primary key field in MySql?
<?phpfunction mysql_next_id($table) {
$result = mysql_query('SHOW TABLE STATUS LIKE "'.$table.'"');
$rows = mysql_fetch_assoc($result);
return $rows['auto_increment'];
}
?>
Programmers try other ways to retrieve the next auto-generated id and fail to get correct value.
Example:
Most of the programmers try to get the MAX(id)+1 (or sort the table in decending order and get the first id) but this will not to work when you delete the last record.
There is a possibility that other users have entered record in the meantime, in this case the value that you have retrieved would be incorrect. You may use LOCK TABLES to prevent such a case.
However, the best thing is to avoid such logics.

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home