[Resolved] Getting latest id
Moderator: General Moderators
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
[Resolved] Getting latest id
Hi,
I have a database with id int unsigned not null auto_increment so the id increments automatically by 1 each time i insert something.
My questions is. I have 10 iteams in my db, when I delete the latest one, I have 9 items but the latest id is still 10. So how do I get this latest id?
Now I have used
SELECT id FROM table ORDER BY id DESC;
And then my $latest_id = $result +1;
But if I have deleted the latest row, then my biggest id would be 9 although my latest id is 10.
Help?
I have a database with id int unsigned not null auto_increment so the id increments automatically by 1 each time i insert something.
My questions is. I have 10 iteams in my db, when I delete the latest one, I have 9 items but the latest id is still 10. So how do I get this latest id?
Now I have used
SELECT id FROM table ORDER BY id DESC;
And then my $latest_id = $result +1;
But if I have deleted the latest row, then my biggest id would be 9 although my latest id is 10.
Help?
Last edited by kaisellgren on Wed Jun 06, 2007 5:47 am, edited 1 time in total.
Re: Getting latest id
You don't. Just let it be. That's what the auto in autoincrement stands for. Let mysql handle it and do not fiddle with that value - unless there's a good, a very good reason.kaisellgren wrote:My questions is. I have 10 iteams in my db, when I delete the latest one, I have 9 items but the latest id is still 10. So how do I get this latest id?
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: Getting latest id
Well I need to know that!volka wrote:You don't. Just let it be. That's what the auto in autoincrement stands for. Let mysql handle it and do not fiddle with that value - unless there's a good, a very good reason.kaisellgren wrote:My questions is. I have 10 iteams in my db, when I delete the latest one, I have 9 items but the latest id is still 10. So how do I get this latest id?
Because I upload a file a zip file.
it will be like this:
1.zip
2.zip
3.zip
...
When I delete the file 10.zip, and I upload new, the newest uploaded file will be also 10.zip ! I need it to be 11.zip. Hopefully you got this...
Not with an autoincrement value.kaisellgren wrote:When I delete the file 10.zip, and I upload new, the newest uploaded file will be also 10.zip !
That's what autoincrement does. It gets incremented each time you insert a record not matter what records you've deleted, always up by one.kaisellgren wrote:I need it to be 11.zip.
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Yes but how do I get the upcoming ID value?volka wrote:Not with an autoincrement value.kaisellgren wrote:When I delete the file 10.zip, and I upload new, the newest uploaded file will be also 10.zip !That's what autoincrement does. It gets incremented each time you insert a record not matter what records you've deleted, always up by one.kaisellgren wrote:I need it to be 11.zip.
Do I first need to insert my file into db and then use mysql_insert_id and then rename my file ??
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Well.volka wrote:You would have to lock the database table to make this safe since there is always the possibility of a concurrend thread grabbing that id between you first function call and the actual insert operation. It's not worth the efford.
I made it now like this:
1) Inserts data to db
2) Grabs mysql_insert_id();
3) Renames the file
And it works well so I am happy now.
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.