Page 1 of 1

MySQL/PHP question

Posted: Thu Jul 21, 2005 10:08 am
by Eraser
Is there a way to determine the next value of an auto-incremented field in a MySQL database?

Simply checking the highest value of the auto-incremented field at this moment won't work, as the most recently added record could have been deleting again, forming a gap between the currently highest value and the actual next value.

So, is there a way to do this?

Posted: Thu Jul 21, 2005 11:47 am
by diego
I know how it works in Oracle but afaik there's no way to do it in MySQL.

Edit: You 're not talking about the 'LAST_INSERT'-function, which outputs the last used value, right?

Posted: Thu Jul 21, 2005 11:54 am
by Eraser
Nah, I need to know the next value.
I've been able to work around it now but it would be nice to do it the way I originally wanted to, as the functionality slightly differs from what I originally intended.

Posted: Thu Jul 21, 2005 12:46 pm
by diego
How did you do it?

Posted: Thu Jul 21, 2005 1:29 pm
by 4days

Posted: Thu Jul 21, 2005 3:26 pm
by glossy
what about:

SELECT id FROM table ORDER BY id DESC LIMIT 1

that will return a single row with only the 'id' of the highest value (and the next auto-incremented value doesn't fill in gaps, but goes from the highest value+1, (unless you've run out of room? unsure))


nevermind, 4days hit the nail most definately on the head there

Posted: Thu Jul 21, 2005 5:23 pm
by dmmh
why do you need to know it anyway? you dont need to hard write it into the DB.