SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Autonumber

  1. #1
    SitePoint Zealot
    Join Date
    May 2001
    Location
    UK
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Autonumber

    OK, I have "ID" as an autonumber. So when I enter the customers information into the database I don't need to put ID. However, after I have done that I want to send them an email with ID=x in the URL, however how can I find out their ID number / is it possible?

    I guess I could search for the information I just added, but there could be multiples the same, that is the whole point of ID.

    Is there some way to find out what current autonumber is up too?

    I'm sure many people would need to do the same?

    Thanks guys
    Please take a minute to do The Ecommerce Survey
    You get a free information security report as a thank you!
    [ More info on report | The Ecommerce Survey ]

  2. #2
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysql_insert_id() will give you the last id that was inserted into the db.
    ck :: bringing chris to the masses.

  3. #3
    SitePoint Member
    Join Date
    Aug 2001
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But if several people have concurrently added a record it could get messed up and send the wrong info to the wrong place right? Do you have a unique id? if so you could select the record with the correct unique id to verify it... Hope I said all that right.

  4. #4
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, mysql_insert_id() is separate for each connection so another connection won't mess it up. And he doesn't have the id anyway: it's being auto_incremented.
    ck :: bringing chris to the masses.

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    dont forget this:

    mysql_insert_id() converts the return type of the native MySQL C API function mysql_insert_id() to
    a type of long. If your AUTO_INCREMENT column has a column type of BIGINT, the value returned by
    mysql_insert_id() will be incorrect. Instead, use the internal MySQL SQL function LAST_INSERT_ID().

    (quote from php reference)

    and you should pass a link reference to mysql_insert_id() so that it does not get use someone elses id (not likely, but possible)...here is the function reference for mysql_insert_id():


    mysql_insert_id (PHP 3, PHP 4 )
    Get the id generated from the previous INSERT operation
    int mysql_insert_id ([int link_identifier])
    mysql_insert_id() returns the ID generated for an AUTO_INCREMENT column by the previous INSERT query using the
    given link_identifier. If link_identifier isnít specified, the last opened link is assumed.
    mysql_insert_id() returns 0 if the previous query does not generate an AUTO_INCREMENT value. If you need to save
    the value for later, be sure to call mysql_insert_id() immediately after the query that generates the value.
    Note: The value of the MySQL SQL function LAST_INSERT_ID() always contains the most recently generated
    AUTO_INCREMENT value, and is not reset between queries.
    signature


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
  •