returning next autoindex id

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crimius
Forum Commoner
Posts: 28
Joined: Sun Oct 13, 2002 6:02 pm
Location: Austin, Texas
Contact:

returning next autoindex id

Post by crimius »

i've found a PHP function (mysql_insert_id) that will tell you the autoincrement value used by the current query, but can't figure out how to determine the last used auto_increment key if i haven't made a query.

i just want to query a table and determine what the next key will be so i can auto populate a form.

Code: Select all

$next_autoindex_id = mysql_insert_id();
	echo ($next_autoindex_id + 1);
this just returns (0 + 1) so it's not really helpful. does anyone know how to query the table for the last used auto_increment key?

thanks in advance
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

As you've found mysql_insert_id() can only be used after an INSERT, but you can query the database to work out what the max value of the auto_increment field:

Code: Select all

SELECT MAX(ID) AS max FROM table
(where ID is the field who's maximum value you want to return)

Mac
crimius
Forum Commoner
Posts: 28
Joined: Sun Oct 13, 2002 6:02 pm
Location: Austin, Texas
Contact:

Post by crimius »

thanks twigletmac :)
Post Reply