Finding id of new row after insert

Discussion in 'PHP' started by Darkhodge, Mar 11, 2007.

  1. #1
    Hey,


    I have a table with the primary key being an auto-increment integer ID.

    If I insert a new record using something like this:

    
    $conn = mysql_connect ('localhost' , $dbUser , $dbPass);
    $db    = mysql_select_db ($dbName , $conn);
    
    $sql     = "INSERT into table_name VALUES (NULL , 'value 1', 'value 2' etc...)";
    $result = mysql_query ($sql , $conn);
    
    PHP:
    What would be the best way to find out the ID of the new record?


    Thanks,

    Hodge :)
     
    Darkhodge, Mar 11, 2007 IP
  2. danzor

    danzor Peon

    Messages:
    208
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #2
    mysql_insert_id()
     
    danzor, Mar 11, 2007 IP
  3. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I think you're looking for the mysql_insert_id() function.
     
    TwistMyArm, Mar 11, 2007 IP
  4. Darkhodge

    Darkhodge Well-Known Member

    Messages:
    2,111
    Likes Received:
    76
    Best Answers:
    1
    Trophy Points:
    185
    #4
    Awesome that's exactly what I need - thanks :)
     
    Darkhodge, Mar 11, 2007 IP
  5. jitesh

    jitesh Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Select Last_inserted_id();
     
    jitesh, Mar 13, 2007 IP
  6. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #6
    I used to query the database to get the id. Thanks a lot. A very useful function :)
     
    Aragorn, Mar 14, 2007 IP