SHARE

How to Determine Auto Increment value of a table MYSQL

Method 1:

SELECT AUTO_INCREMENT FROM information_schema.`TABLES` WHERE `TABLE_NAME` LIKE 'table_name'

Code PHP:

$table_name = 'wp_posts';
$result = mysql_query("SELECT AUTO_INCREMENT FROM information_schema.`TABLES` WHERE `TABLE_NAME` LIKE '{$table_name}'");
$row = mysql_fetch_array($result);
$nextId = $row[0];
mysql_free_result($result);

Result:

84586
#Showing rows 0 – 0 ( 1 total, Query took 0.0009 sec)

Method 2:

SHOW TABLE STATUS LIKE 'table_name'

Code PHP:

$table_name = 'wp_posts';
$result = mysql_query("SHOW TABLE STATUS LIKE '$table_name'");
$row = mysql_fetch_array($result);
$nextId = $row['Auto_increment'];
mysql_free_result($result);

Method 3:

SELECT MAX(id) as max FROM `table_name`
$table_name = 'wp_posts';
$result = mysql_query("SELECT MAX(id) as max FROM `$table_name`");
$nextid = mysql_fetch_assoc($result);
if($nextid['max']!=null){
    $nextid = $nextid['max'] + 1;
}else{
    $nextid = 1;
}
mysql_free_result($result);

Method 4:

SELECT id FROM `table_name` ORDER BY id DESC LIMIT 1
$table_name = 'wp_posts';
$result = mysql_query("SELECT id FROM `$table_name` ORDER BY id DESC LIMIT 1");
if(mysql_num_rows($result)>0){
    $nextid = mysql_fetch_assoc($result);
    $nextid = $nextid['id'] + 1;
}else{
    $nextid = 1;
}
mysql_free_result($result);

Method 5:

insert into `table_name` values(...);
select LAST_INSERT_ID() as last_insert_id;

You must increase the value by one.

Method 6 (same with method 5):

insert into `table_name` values(...);
select LAST_INSERT_ID()+1 as nextid;

Method 7:

SHOW CREATE TABLE `table_name`

Result:

CREATE TABLE `wp_posts` (
`ID` bigint(20) unsigned NOT NULL auto_increment,
`post_author` bigint(20) unsigned NOT NULL default ‘0’,
`post_date` datetime NOT NULL default ‘0000-00-00 00:00:00’,
`post_date_gmt` datetime NOT NULL default ‘0000-00-00 00:00:00’,
`post_content` longtext NOT NULL,
`post_title` text NOT NULL,
`post_excerpt` text NOT NULL,
`post_status` varchar(20) NOT NULL default ‘publish’,
`comment_status` varchar(20) NOT NULL default ‘open’,
`ping_status` varchar(20) NOT NULL default ‘open’,
`post_password` varchar(20) NOT NULL default ”,
`post_name` varchar(200) NOT NULL default ”,
`to_ping` text NOT NULL,
`pinged` text NOT NULL,
`post_modified` datetime NOT NULL default ‘0000-00-00 00:00:00’,
`post_modified_gmt` datetime NOT NULL default ‘0000-00-00 00:00:00’,
`post_content_filtered` longtext NOT NULL,
`post_parent` bigint(20) unsigned NOT NULL default ‘0’,
`guid` varchar(255) NOT NULL default ”,
`menu_order` int(11) NOT NULL default ‘0’,
`post_type` varchar(20) NOT NULL default ‘post’,
`post_mime_type` varchar(100) NOT NULL default ”,
`comment_count` bigint(20) NOT NULL default ‘0’,
PRIMARY KEY (`ID`),
KEY `post_name` (`post_name`),
KEY `type_status_date` (`post_type`,`post_status`,`post_date`,`ID`),
KEY `post_parent` (`post_parent`),
KEY `post_author` (`post_author`)
) ENGINE=MyISAM AUTO_INCREMENT=84586 DEFAULT CHARSET=utf8

Code PHP:

$table_name = "wp_posts";
$result = mysql_query("SHOW CREATE TABLE `$table_name`");
$row = mysql_fetch_array($result);
$nextid = preg_replace("/^.*?AUTO_INCREMENT=(\d+).*?$/s","$1", $row[1]);

Result:

84586

Method 8:
PHP:

mysql_insert_id();

Example:

$table_name = "wp_posts";
$result = mysql_query("INSERT INTO `$table_name` values (......)");
$nextid = mysql_insert_id()+1;

NO COMMENTS