You are here
Home > Php > Next Increment Id in MySQL using PHP

Next Increment Id in MySQL using PHP

Hello Friends, Today I am going to share that how to get next increment id in mysql using php. Actually MySQL has AUTO_INCREMENT keyword to perform auto increment on a column value. AUTO_INCREMENT by default starts from 1 and It is incremented by 1 for each new record. To get next auto increment value in MySQL we can use LAST_INSERT_ID() function or AUTO_INCREMENT with SELECT but in PHP we can call insert_id on connection object or mysqli_insert_id(connection). But both PHP method is good when you have just executed insert query and it gives last insert id which we can use after incrementing it by 1 to get next increment id. But what if we have not done any insert query and still want next increment id of specific table. Then we can use AUTO_INCREMENT with SELECT.

The following is the SQL syntax to get the next id.

PHP Code

 Data of Table “test”

id name
1 Vivek
2 Suresh

Output

Next increment id for table test will be: 3

Thanks

Please share if you like it

Rajesh Kumar Sahanee
I am a passionate Java Developer and I like Computer Programming. I love to do some interesting experiments and listening music in my free time.
https://www.zatackcoder.com

Comments

zatackcoder
Top