PHP: three methods to obtain the ID value of the new MySql record _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP can obtain the ID of a new MySql record. 1. use the statement: Copy the code as follows: mysql_query (selectmax (id) fromt1, $ link); use this method to obtain the largest value of id, which is indeed the last value, however, when multiple threads are connected I. statement:

The code is as follows:

Mysql_query ("select max (id) from t1", $ link );


This method is used to obtain the largest value of id, which is indeed the last value. However, when multiple threads are connected, this largest id is not necessarily the auto-incremental id value of the inserted data, therefore, it is not applicable to multithreading.

2. use the function: msyql_insert_id ();

In PHP, the id value inserted into the database is often taken out, and there is such a function:

The code is as follows:


<? Php
// Execute the statement for inserting the database
//......
$ GetID = mysql_insert_id (); // $ getID is the ID of the last record

// Conditions for using this function:
// 1. assume that the field name is recordID
// 2. set the field attribute to auto_increment.
// 3. use it after adding data
// $ NewID = mysql_insert_id ();
// Obtain the ID value
?>

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

After the system executes the INSERT statement and then executes the SELECT statement, it may have been distributed to different backend servers. If php is used for programming, you should use mysql_insert_id () to obtain the latest inserted id, after each INSERT operation, the corresponding autoincrement value is calculated and returned to PHP. you do not need to issue an independent query and use mysql_insert_id () directly.
When a statement is inserted, it automatically returns the last id (mysql auto-increment ).
And this function is only useful for the current link, that is, it is a multi-user security type.
We recommend that you use this function;
Problem: It does not work when the id is bigint.

III. use query

The code is as follows:

Msyql_query ("select last_insert_id ()");


Last_insert_id () is a mysql function that also takes effect on the current link.
This method solves the bigint type problem in mysql_insert_id ().
Summary:
Method 2 is recommended. in special cases, you can consider method 3.

Example code: mysql_query ("select max (id) from t1", $ link); use this method to obtain the largest value of id, which is indeed the last value, but when multiple linked threads...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.