3 ways to get the ID value of new MySql records in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 10:26:06
Original
800 people have browsed it

1. Use sentences:

Copy code The code is as follows:
mysql_query("select max(id) from t1",$link);

What you get using this method is the largest ID value, which is indeed the last value. However, when there are multiple link threads, this largest ID is not necessarily the auto-increment ID value of the data we insert, so it is not suitable for multi-threading.

Two, use the function: msyql_insert_id();

In PHP, it is often necessary to retrieve the id value inserted into the database, and there is exactly such a function:
Copy code The code is as follows:

//Execute the statement inserted into the database
//……
$getID=mysql_insert_id();//$getID is the ID of the last record

//Conditions for using this function:
//1. Assume the field name is recordID
//2. The field attribute must be set to: auto_increment
//3. Use
after adding data //$newID = mysql_insert_id();
//Get ID value
?>

The PHP function mysql_insert_id() returns the value of the field defined by AUTO_INCREMENT after the last INSERT query was executed.

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 at this time. After each INSERT is completed, , in fact, the corresponding autoincrement value has already been calculated and returned to PHP. You do not need to issue a separate query, just use mysql_insert_id().
When a statement is inserted, 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 type bigint, it no longer works.

3. Use query

Copy code The code is as follows:
msyql_query("select last_insert_id()");

last_insert_id() is a function of mysql and is also effective on 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824827.htmlTechArticle1. Use statements: Copy the code as follows: mysql_query("select max(id) from t1",$link ); What you get using this method is the largest value of id, which is indeed the last value, but when multiple link threads...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!