Get Unique ID after inserting a row

Discussion in 'PHP' started by Christian Little, Nov 3, 2008.

  1. #1
    Here's a question for the pros - let's say I have a MySQL table that had a unique int for the primary key that is auto_increment.

    Whenever a row gets inserted, I want to get the unique ID for that specific row.

    This is for a click tracking program I'm building, each time somebody clicks something the code fires and I need to get the unique ID that get's inserted.


    So we get something like this:

    1) Page Loads and fires the tracking script
    2) Tracking script inserts a row into the table
    3) Tracking script needs the unique ID for the row

    How do I get the unique ID? Is there some setting in the insert query where it will return the ID?
     
    Christian Little, Nov 3, 2008 IP
  2. dev_SeeInside

    dev_SeeInside Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    after the script inserts the new row, use

    
    $row_id = mysql_insert_id();
    
    Code (markup):
    That should give you the ID of the last row inserted

    ca3.php.net/mysql_insert_id
     
    dev_SeeInside, Nov 3, 2008 IP
    Christian Little likes this.
  3. Christian Little

    Christian Little Peon

    Messages:
    1,753
    Likes Received:
    80
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Jeebus I can't believe I've not heard of that one before. That makes things way easier lol, thanks dude :)
     
    Christian Little, Nov 3, 2008 IP
  4. dev_SeeInside

    dev_SeeInside Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    lol no problem man, glad I could help :)
     
    dev_SeeInside, Nov 3, 2008 IP