SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast Vman's Avatar
    Join Date
    Feb 2001
    Location
    Australia
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mysql_insert_id(); not returning the correct value

    My PHP code was working fine when I added something I called mysql_insert_id(); and it gave me the correct - I deleted some rows and now it is giving an incorrect ID.

    Does anyone know if there is a way to get it to return the correct value?

    Many thanks.

  2. #2
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what do you mean, "correct" or "incorrect?" if you deleted some rows, the AUTO_INCREMENT number is not set to a lower value -- it always goes up. this is how it's supposed to be. so i assume you're still getting a number, but it's just higher than you think it should be? then, yep, that's the correct behavior.

    if you DO want to reset the AUTO_INCREMENT back to something lower (make sure it's a least 1 more than the currently highest value), use ALTER TABLE:

    Code:
    ALTER TABLE table AUTO_INCREMENT=n
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  3. #3
    SitePoint Enthusiast Vman's Avatar
    Join Date
    Feb 2001
    Location
    Australia
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The autoincrement is working fine. It keeps going up in a sequential order.

    Say I insert an new row into the table and then call mysql_insert_id(); to get the id - it will return a value of 24 when the actual value is 19 - there is no row in the table that has a value of 24 there are only 19 rows and they are numbered 1-19.

    As I deleted 5 rows it appears that mysql_insert_id(); is getting the number from somewhere else and not that actual row id.

  4. #4
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Vman
    Say I insert an new row into the table and then call mysql_insert_id(); to get the id - it will return a value of 24 when the actual value is 19 - there is no row in the table that has a value of 24 there are only 19 rows and they are numbered 1-19.
    when it returns the value of 24, are you SURE that there isn't then a row with a value of 24 (it doesn't matter if there are only 19 rows)? if not, why don't you post your table structure (CREATE TABLE statement) and your relevant PHP code.

  5. #5
    SitePoint Enthusiast Vman's Avatar
    Join Date
    Feb 2001
    Location
    Australia
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahhh I see now - I had an error in my code.
    I was passing the wrong variable.

    Many Thanks for you help DR_LaRRY_PEpPeR

    Now I just need to replace all my hair that I lost trying to figure this out for the past 2 days.

  6. #6
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i HATE when that happens. glad you have it working!


Bookmarks

Posting Permissions

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