1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

MySQL: Retrieve the last added row

Discussion in 'PHP' started by m0nkeymafia, Aug 5, 2008.

  1. #1
    Apologies for the slightly misleading title I didn't know how to succinctly describe my problem.

    I would like to get the details, specifically primary key, of the row I have just inserted. I could quite easily check the last inserted row AFTER I have inserted it, but what would happen if another row was added before I did that?

    Does anyone know how to do this?

    Thanks
     
    m0nkeymafia, Aug 5, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    You can use mysql_insert_id() to retrieve the id of the last inserted row - http://us.php.net/mysql_insert_id.

    As long as you are using the same connection there's no chance of retrieving the wrong row regardless of what else is happening at the same time.
     
    jestep, Aug 5, 2008 IP
    m0nkeymafia likes this.
  3. yleiko

    yleiko Peon

    Messages:
    74
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    there is a native function
    mysql_insert_id()
     
    yleiko, Aug 5, 2008 IP
  4. rob_v

    rob_v Peon

    Messages:
    72
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    for mysql I think it's get_last_rowid()

    That will give you the last row id of that txn. If another process does an insert in the mean time it will still return that txn id that was inserted.
     
    rob_v, Aug 5, 2008 IP
  5. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #5
    You only work with your own session. If anyone add another row to the database before you retrieve your last row id, it doesn't affect you. You can still retrieve your last row id.

    I assume you use something like mysql_insert_id() to retrieve your last insert row ID.
     
    php-lover, Aug 5, 2008 IP
  6. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #6
    Thanks guys, appreciate the replies.

    Out of interest, if the page gets called twice as the same time, will it process them sequentially then, or in parallel? I assumed parallel hence my worry about getting the last ID.
     
    m0nkeymafia, Aug 5, 2008 IP
  7. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #7
    Should still work fine. The last_id is retrieved based on the connection being used. A single script could make an insert and then retrieve the correct id even if the script took five minutes to process between the insert and the retrieval.

    Not positive on this, but if you are using a singleton pattern for your database connection, then it may be possible to get an incorrect id.
     
    jestep, Aug 5, 2008 IP