MySQL/PHP question

Open discussion about any topic, as long as you abide by the rules of course!
Post Reply
User avatar
Eraser
Posts: 19181
Joined: Fri Dec 01, 2000 8:00 am

MySQL/PHP question

Post 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?
diego
Posts: 1379
Joined: Mon Jan 24, 2005 12:25 pm

Post 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?
User avatar
Eraser
Posts: 19181
Joined: Fri Dec 01, 2000 8:00 am

Post 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.
diego
Posts: 1379
Joined: Mon Jan 24, 2005 12:25 pm

Post by diego »

How did you do it?
4days
Posts: 5465
Joined: Tue Apr 16, 2002 7:00 am

Post by 4days »

glossy
Posts: 2282
Joined: Tue Apr 30, 2002 7:00 am

Post 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
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Post by dmmh »

why do you need to know it anyway? you dont need to hard write it into the DB.
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
Post Reply