Results 1 to 8 of 8

Thread: MySQL: ID of Last Record?

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    MySQL: ID of Last Record?

    How can I retrieve the ID of the last record in my table?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    mysql_result("select id from tableName",mysql_numrows("select id from tableName") -1,"id");

    That should do it.
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  3. #3

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Thanks, I'll give it a try.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    That will only work if you have id as the primary.

    Also you may want to attempt:
    PHP Code:
    $query mysql_query("select id from tablename order by id desc");
    mysql_result ($query,0,"id"); 
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  5. #5

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    This works for me:

    PHP Code:
    $q "SELECT id FROM newsitems ORDER by id ASC LIMIT 1, 1";
    $id mysql_fetch_array(mysql_query($q));
        
    echo 
    $id[0]; 
    Thanks for your help.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    btw, very nice site. It's definitely one for my bookmark's, you do have a typo though: "prever" should be "prefer" i believe.

    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  7. #7
    scoutt
    Guest
    why don't use just use

    LAST_INSERT_ID()

    always contains the most recently generated AUTO_INCREMENT value, and is not reset between queries

    or

    mysql_insert_id()

    Returns the ID generated for an AUTO_INCREMENT column by the previous query. Use this function after you have performed an INSERT query into a table that contains an AUTO_INCREMENT field.

  8. #8

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    But weeks could go by before the script is run again. So LAST_INSERT_ID() wouldn't work.
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

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



Click Here to Expand Forum to Full Width