Archives » July 5th, 2006

July 5, 2006

Find Next Autoindex Using PHP and MySQL

I’m putting on my programmer’s hat tonight to solve a mystery. I was searching Google tonight to try to find the way you would use PHP to grab the next AUTO_INCREMENT value out of a MySQL table. You know, if you’re getting ready to insert data into a table, and you want to know beforehand what the next autonumber is going to be on the ID field. I found the site that shows you how to do it.

$tablename = "tablename";
$next_increment = 0;
$qShowStatus = "SHOW TABLE STATUS LIKE ’$tablename’";
$qShowStatusResult = mysql_query($qShowStatus) or die ( "Query failed: " . mysql_error() . "
" . $qShowStatus );

$row = mysql_fetch_assoc($qShowStatusResult);
$next_increment = $row[’Auto_increment’];

echo "next increment number: [$next_increment]";

Just jotting this down here as a note so I can remember for next time.