Q:

How efficient is Last_insert_id?

I recently came across a problem where I needed to be able to grab the last ID of a query I've inserted on a page. The process was quite simple, but I'd like some reassurance on how the process is done. Here's how my code is:
$query30 = $db->query("INSERT `mybb_quickexample` SET `player1` = '".$row19[username]."', `player2` = '".$row18[username]."', `games` = '".$numberofgames."'")or die(       mysqli_error());

As you can see  I have just inserted something into mybb_quickexample, which then generates an ID. Now in the code directly under it, I try to locate the ID.
$mmid = $db->insert_id(); 

foreach($_POST['optiontitle'] as $val){
$val = $db->escape_string($val); //
echo $val;
$query30 = $db->query("INSERT `mybb_quickexample2` SET `gid` = '".$id."', `mid` = '".$mmid."', `optiontitle` = 'OPTION TITLE', `option1` = '$val'")or die(      mysqli_error());

}

As you can see, it grabs the recent ID and puts it into the new table. Allowing for these 2 tables to correspond with each other later on when I need them to.
However, does insert_last_id grab the last row inserted into the database or the last row inserted into the database on the page? Now I'm not entirely sure of the mechanics behind this, but I ask this because, what if a person was to load this page at the same exact time as another person? Would there be unmatched ID grabs or would the code still work?

A:

insert_last_id returns the last inserted ID of the very same SQL session/connection (see MySQL manual). - The SQL server does not know about your pages. As long as the connection/session is not shared there are no problems (such as race conditions).
It's also possible to get the last inserted ID using SQL only: select LAST_INSERT_ID(); or use it directly in a following SQL query in order to reduce client-server round-trips like insert table B set foreign_id=LAST_INSERT_ID(),data="something"; (note, however, if the following SQL statement also has an auto increment column the next LAST_INSERT_ID() call would return the newest inserted ID; see MySQL manual)

A:

Others have pointed out that the LAST_INSERT_ID is maintained per MySQL connection. It captures the actual id used in the most recent insertion with an autoincrementing value in your connection.  That is, it does the right thing even if other connections are also doing lots of insertions. (That might be true on a busy web site serving multiple users concurrently.)
If you need to use it in an insert, you can do that directly without any sort of round trip to the client.  For example, here are two queries you could issue one after the other from your program
INSERT INTO person (first, last) 
            VALUES ('Jack', 'Straw');

INSET INTO address (person_id, addr, town) 
            VALUES (LAST_INSERT_ID(),'10 Downing St', 'London');

See how that works? You can use it to supply a value in an insert value list. It's one of the simplest and most efficient things in MySQL when you use it that way.

