Alaa M. Jaddou Alaa M. Jaddou - 6 months ago 10
SQL Question

get next auto_increament value without insertion

I have a table like this

| id | name |
| 1 | Alaa |
| 2 | Mohd |


and so on.
id is auto increament value.

if i delete the second row the table will be like this

| id | name |
| 1 | Alaa |


but the auto_increament next value will be 3.

now what i need, i need to get the next auto increament value with codeigniter.
don't tell me to use

$this->db->insert_id();


THERE IS NO INSERTION

I NEED TO GET THE VALUE WITHOUT ANY INSERTION.

Answer

You can grab that from information_schema see example below, just replace tablename and databasename with actual values.

SELECT AUTO_INCREMENT FROM information_schema.tables
WHERE table_name = 'tablename' AND table_schema = 'databasename'

Note: Make sure your table have AUTO_INCREMENT column, otherwise you will get null.

Comments