Page 1 of 1

returning next autoindex id

Posted: Sat Apr 19, 2003 2:49 pm
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

Posted: Sat Apr 19, 2003 3:18 pm
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

Posted: Sat Apr 19, 2003 3:21 pm
by crimius
thanks twigletmac :)