Pages

Your Ad Here

This Blog is not to read or go through

because, I have never been such a mess


Search the blog instead

Thursday, April 30, 2009

select maximum value from an auto increment field

$table_name = "jos_menu";
$resultset = mysql_query("desc $table_name");
while($a = mysql_fetch_assoc($resultset))
{
if($a["Extra"] == "auto_increment"){
$query = "SELECT max(".$a["Field"].") as auto from $table_name";
$resultset1 = mysql_query($query);
$answer = mysql_fetch_assoc($resultset1);
die($answer["auto"]);
break;
}
}

Note: It can be done just by subtracting 1 from the current Auto_increment value in a table. But, I never found this justifiable because what if the last row is deleted.

No comments:

Your Ad Here