There are two ways to call this method:
- Object Oriented -
$mysqli->insert_id ; - Procedural (as you indicated) -
mysqli_insert_id(mysqli $link)
In both cases, the last insertion identifier refers to the connection you established with the database. For explanation, although I believe that the object-oriented approach provides more clarity. Consider the following code:
$mysqli = new mysqli('host','username','password','database'); // Insert into table with an AUTO INCREMENT field defined` if ($result = $mysqli->query("INSERT INTO table (somefield) VALUES ('somevalue');")) { echo 'The ID is: '.$mysqli->insert_id; }
The instance variable $insert_id updated whenever the request function is called. Thus, the retained value is only within your code, and not in relation to other interactions with the database by any other process.
There are some interesting points to keep in mind. For example, if you decide to do bulk inserts, for example.
$mysqli->query("INSERT INTO table (somefield) VALUES ('somevalue'), ('anothervalue');")
It will return the identifier of the first row inserted, not the last.
For further reading, PHP Doc provides more clarification.
source share