How to get the next auto_increment ID from Mysql Table

 

Use this php function to get the next auto_increment ID from a MySQL Table:

Function:


function get_next_auto_id($tablename, $id){
$sql     = "select $id from $tablename order by $id desc limit 1";
$query     = mysql_query($sql);
$row     = mysql_fetch_assoc($query);
return $row[$id] + 1;
}

Usage Example:

Lets say you have a table like below: (tablename: siteinfo):

site_id

title

logo

meta_description

meta_keywords

1

Title1

Some.jpg

Meta description goes here Collections of Film Tricks.

2

Title2

Some2.jpg

2 description goes here Asdf asdfasd asdfas

<?php

function get_next_auto_id($tablename, $id){
$sql     = "select $id from $tablename order by $id desc limit 1";
$query     = mysql_query($sql);
$row     = mysql_fetch_assoc($query);
return $row[$id] + 1;
}

$next_id = get_next_auto_id('siteinfo'',site_'id');

echo $next_id;

?>

This above example will give the result : 3

I hope this helps everyone!.

Add a Comment

Your email address will not be published. Required fields are marked *