Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2002

    Unanswered: mysql_insert_id question

    let's consider the example code of mysql_insert_id I take from

    mysql_connect("localhost", "mysql_user", "mysql_password") or
    die("Impossible de se connecter : " . mysql_error());

    mysql_query("INSERT INTO mytable (product) values ('kossu')");
    printf ("Le dernier ID ins&eacute;r&eacute; dans est le %d\n", mysql_insert_id());

    Assuming that there are two processes (A & B) that are concurrently executing the above script. WHat I am not clear is the race condition problem. When A finishes the line "mysql_query..", the OS switches to process B. B also executes until it finishes the line "mysql_query..". And at this time, the OS switches back to A. A goes on with "printf.." line. So when A calls mysql_insert_id , which id is returned : of the record inserted from A or from B ?

  2. #2
    Join Date
    Aug 2003
    Bologna - Italy
    You analisys is wrong.

    mysql_insert_id has a parameter, which can be omitted (the last opened connection handle would be used), which is the DB connection handle, which is returned by mysql_connect, that your code discards.

    So, if another process would call the function, it will surely have a different db connection handle, so no confusion arises.
    The only failure is not trying to do it.

Posting Permissions

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