SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    London
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mysql_insert_id() question

    Is this a good way to determine the auto_incremented id of the insert just performed in the script? I have a system where a number of clients are doing inserts at the same time and I don't know whether this will be a problem. Even if I call it right after the mysql_query that does an insert, what if if between the insert and the mysql_insert_id() call there is another insert by another client?

  2. #2
    SitePoint Enthusiast Homie_187's Avatar
    Join Date
    Oct 2008
    Location
    United States
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was just wondering about this myself, and from what I've read it seems it is supposed to work okay. The function returns the last id inserted via the connection that is passed as the argument. I guess since there is a different connection for each client there is no way for other insertions to interfere.

    I guess it's a pretty common question. More info here:
    http://forums.mysql.com/read.php?52,186749,186749

    I was also wondering if there was a way that you could get the last inserted id within the SQL query itself and possibly insert that into another table with a subquery(?)

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,868
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The call returns the id that was last inserted by the current client. It ignores any subsequent inserts by other clients.

    I don't think you can do it all in one query since you wouldn't have control of the execution order to ensure that the autoincrement value is assigned first before trying to copy it.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Homie_187 View Post
    I was also wondering if there was a way that you could get the last inserted id within the SQL query itself and possibly insert that into another table with a subquery(?)
    no. you can only insert in to one table at a time.
    Check out our new Industry News forum!
    Keep up-to-date with the latest SP news in the Community Crier

    I edit the SitePoint Podcast


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
  •