Web Hosting Talk







View Full Version : How do I get the next ID?


JoeBannon
07-15-2006, 01:48 AM
How can I get the next ID to be generated for a table with AUTO_INCREMENT?

Jatinder
07-15-2006, 02:02 AM
Assuming that auto_id is the name of the column with AUTO_INCREMENT, you can use

SELECT max(auto_id)+1 AS nextid FROM your_table;

I am not sure why you need this. But a better approach would be to run the insert query and then use mysql_insert_id() to get the value of AUTO_INCREMENT column.

serversphere
07-15-2006, 02:02 AM
Are you talking PHP?

$insert_id = mysql_insert_id();

martialtiger
07-15-2006, 02:04 AM
I'm fairly new to programming but if you are using PHP you can get the value like so:

$next_id = mysql_insert_id();

Good luck!