Home  >  Article  >  Backend Development  >  php gets the id value of mysql inserted data

php gets the id value of mysql inserted data

WBOY
WBOYOriginal
2016-07-25 08:54:481001browse
  1. mysql_query("select max(id) from t1",$link);
Copy code

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

  1. msyql_query("select last_insert_id()");
Copy code

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.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn