Forum Moderators: coopster

Message Too Old, No Replies

How to find auto increment value for table in PHP

A solution snippet which might be useful

         

vincevincevince

5:30 am on Jan 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



function get_auto_increment($table_name,$link_identifier=false)
{
if ($link_identifier) $dh=mysql_query("SHOW TABLE STATUS",$link_identifier);
else $dh=mysql_query("SHOW TABLE STATUS");
while ($row=mysql_fetch_assoc($dh))
{

if ($row[Name]==$table_name) 
{
return $row[Auto_increment];
break;
}
[red]

}[/red]
return false;
}

Useful in conjunction with

ALTER TABLE $table_name AUTO_INCREMENT $newvalue

Usage:
Find out the next auto_increment value for `test` table:

print "Next test ID will be: ".get_auto_increment("test")

eelixduppy

1:50 pm on Jan 17, 2008 (gmt 0)



Thanks for that little snippet, vincevincevince. :)