PHP 3 ways to get MySQL new record ID value _php tips

Source: Internet
Author: User

First, the use of statements:

Copy Code code as follows:
mysql_query ("SELECT Max (ID) from T1", $link);

Using this method, the maximum ID value is the last value, but when a multilink connection is used, the maximum ID is not necessarily the ID value of the data we insert, so it does not apply to multithreading.

two, use function: msyql_insert_id ();

In PHP, it is often necessary to remove the ID value from the Insert database, and there is just one function:
Copy Code code as follows:

<?php
Executing statements that insert a database
......
$getID =mysql_insert_id ();//$getID is the ID of the last record

Conditions used for this function:
1. Suppose the field name is RecordID
2. The field property must be set to: auto_increment
3. Use after adding data
$newID = mysql_insert_id ();
Get ID Value
?>

PHP function mysql_insert_id () is the value of the field defined by Auto_increment after the last time an insert query was executed.

When the system completes the insert, and then executes the SELECT, it may have been distributed to different back-end servers, and if you are programming with PHP, you should get the newly inserted ID through mysql_insert_id (), each time the insert ends, In fact, the corresponding AutoIncrement value has been calculated to return to PHP, you do not have to issue a separate query, directly with mysql_insert_id () can be.
When you insert a statement, it automatically returns the last ID (MySQL self-increment).
And this function is only useful for the current link, that is, it is multi-user-safe.
It is recommended that this function be used;
Problem: When ID is bigint type, it is not working.

Third, the use of inquiries

Copy Code code as follows:
Msyql_query ("Select last_insert_id ()");

LAST_INSERT_ID () is a MySQL function is also valid for the current link
This usage solves the bigint type problem encountered in mysql_insert_id ()
Summarize:
Recommended use of method two, under special circumstances, you can consider method three.

Related Article

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.