Results 1 to 7 of 7

Thread: MySQL: Auto Incrementing [Resolved]

  1. #1

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349

    MySQL: Auto Incrementing [Resolved]

    When using the auto increment ability on my primary key I want to also find out what the next value of it will be. Does anyone know how to do this?

    Thanx
    Last edited by Electroman; Feb 25th, 2004 at 08:58 PM.
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  2. #2
    You could use mysql_insert_id() to grab the last insert ID, then add one to it.

  3. #3

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Not sure how that works, if I added the new record letting the auto increment sort out the id then ran mysql_insert_id() would it return the id I just created?
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by Electroman
    Not sure how that works, if I added the new record letting the auto increment sort out the id then ran mysql_insert_id() would it return the id I just created?
    Yes.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Thank you.

    Have now completed that script, Unique Visitors Counter . I actually worked round it by calculating what it should be, because the number is actually used in the same statement, but I think I'll redo it using this method. Then do an Update statement after it to include it in the record.
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I think there's also LAST_INSERT_ID and NEXT_INSERT_ID functions that you can use in SQL statements.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by CornedBee
    I think there's also LAST_INSERT_ID and NEXT_INSERT_ID functions that you can use in SQL statements.
    Yes, there is. But I always found it easier to just use the PHP function. I guess there's really no difference. I'm sure the PHP function just calls a query for LAST_INSERT_ID.

    A long time ago, Matt, phpman, and I had a big debate about how to do this properly. I don't think we ever resolved anything, but there might be something worthwhile in the thread.
    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