note 86665 added to function.mysql-insert-id

From: Date: Tue, 28 Oct 2008 23:52:12 +0000
Subject: note 86665 added to function.mysql-insert-id
Groups: php.notes 
Request: Send a blank email to php-notes+get-145911@lists.php.net to get a copy of this message
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]"; ?> ---- Server IP: 69.147.83.197 Probable Submitter: 121.97.54.2 (proxied: 121.97.54.207) ---- Manual Page -- http://www.php.net/manual/en/function.mysql-insert-id.php Edit -- https://master.php.net/note/edit/86665 Del: integrated -- https://master.php.net/note/delete/86665/integrated Del: useless -- https://master.php.net/note/delete/86665/useless Del: bad code -- https://master.php.net/note/delete/86665/bad+code Del: spam -- https://master.php.net/note/delete/86665/spam Del: non-english -- https://master.php.net/note/delete/86665/non-english Del: in docs -- https://master.php.net/note/delete/86665/in+docs Del: other reasons-- https://master.php.net/note/delete/86665 Reject -- https://master.php.net/note/reject/86665 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#145911) next »