Home > Article > Backend Development > php gets the id value of mysql inserted data
Using this method, we get the largest value of id, which is indeed the last value, but when there are multiple links When threading, the largest ID is not necessarily the auto-incrementing ID value of the data we insert, so it is not suitable for multi-threading. Second, use functions: msyql_insert_id(); When the system executes INSERT and then executes SELECT, it may have been distributed to different back-end servers. If you use PHP programming, you should use mysql_insert_id() to get the latest inserted id. After each INSERT ends, the corresponding The autoincrement value has been calculated and returned to PHP. You do not need to issue a separate query, just use mysql_insert_id(). When inserting a statement, it automatically returns the last id (mysql auto-increment value). And this function is only useful for the current link, that is, it is multi-user safe. It is recommended to use this function; Problem: When the id is of bigint type, it no longer works. Three, use query
last_insert_id() is a mysql function and is effective for the current link This usage solves the bigint type problem encountered in mysql_insert_id () Summary: It is recommended to use method two. In special circumstances, method three can be considered. |