note 86665 deleted from function.mysql-insert-id by danbrown

From: Date: Wed, 29 Oct 2008 01:55:25 +0000
Subject: note 86665 deleted from function.mysql-insert-id by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-145915@lists.php.net to get a copy of this message
Note Submitter: shelby at coolpage dot com ---- The suggestion to use SELECT MAX(id)+1... to get the next insert id, is erroneous because if any rows have been deleted from the end of the table, then MAX(id) may not equal next AutoIncrement value for the table. Instead I found this with Google, and it apparently works: <? $tablename = "tablename"; $next_increment = 0; $qShowStatus = "SHOW TABLE STATUS LIKE '$tablename'"; $qShowStatusResult = mysql_query($qShowStatus) or die ( "Query failed: " . mysql_error() . "<br/>" . $qShowStatus ); $row = mysql_fetch_assoc($qShowStatusResult); $next_increment = $row['Auto_increment']; echo "next increment number: [$next_increment]"; ?>

« previous php.notes (#145915) next »