We do not often need this but sometimes when we need to retrieve the next auto increment value of a table (without incrementing the auto increment value of course), this solution below will help.
<?php $query = mysql_query("SHOW TABLE STATUS WHERE name='your_table_name_here'");
if (mysql_num_rows($query)) {
$result = mysql_fetch_assoc($query);
echo $result['Auto_increment'];
} else {//error
//error control here
}
?>
Just replace “your_table_name_here” with your mySQL table name.
As a Function
Furthermore, making it a PHP function would make the solution looks clean.
<?php function get_next_value($tablename) { $query = mysql_query("SHOW TABLE STATUS WHERE name='".mysql_real_escape_string($tablename)."'");
if (mysql_num_rows($query)) {
$result = mysql_fetch_assoc($query);
return $result['Auto_increment'];
} else {//error
return false;
}
}
//usage
$nextval = get_next_value('your_table_name_here');
?>
Hope this helps.
Source From: http://zenverse.net/php-mysql-next-auto-increment-value
No comments:
Post a Comment