Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Location
    Finland, Hollola
    Posts
    285
    Thanks
    8
    Thanked 0 Times in 0 Posts

    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?
    PHP 5 & MySQL 5 (Y)

  • #2
    New Coder
    Join Date
    Sep 2006
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    auto increment column types have nothing to do with how many rows are in the table, they are used solely as a unique identifier, where no single row can contain that same value, which makes it unique. But more importantly it's used to reference other tables that have a relationship with that unique number/key!

  • #3
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    use
    $latest_id=mysql_insert_id();
    to get latest id that have been inserted but use it just after the insert query

  • #4
    Regular Coder
    Join Date
    Jan 2006
    Location
    Finland, Hollola
    Posts
    285
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Thanks I have resolved this issue.
    PHP 5 & MySQL 5 (Y)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •