Phpmysql_insert_id () returns the Latest Database id implementation method

Source: Internet
Author: User
Phpmysql_insert_id () returns the Latest Database id implementation method. for details, refer to the code below: mysql_insert_id () function returns... php mysql_insert_id () returns the Latest Database id implementation method. for details, refer to the code below:

Mysql_insert_id ()

The mysql_insert_id () function returns the ID generated by the previous INSERT operation. If no AUTO_INCREMENT ID is generated in the previous query, mysql_insert_id () returns 0.

Syntax:Mysql_insert_id (connection)

The connection parameter (optional) specifies the MySQL connection. if not specified, the previous connection is used.

Mysql_insert_id () returns the AUTO_INCREMENT ID generated in the previous INSERT query in the given connection. If no connection is specified, the previous opened connection is used.

If you want to save the value and use it later, make sure that mysql_insert_id () is called immediately after the query that generates the value. The code is as follows:

 

Mysql_insert_id () returns the AUTO_INCREMENT ID generated in the previous INSERT query in the given link_identifier. if link_identifier is not specified, the previous opened connection is used, if the value of AUTO_INCREMENT is not generated in the previous query, mysql_insert_id () returns 0. if you want to save the value and use it later, make sure that mysql_insert_id () is called immediately after the query that generates the value ().

LAST_INSERT_ID ()

MySQL also provides an API with the same function. it always stores the newly generated AUTO_INCREMENT value and will not be reset between query statements. that is to say, after the INSERT operation is executed, executing SELECT, UPDATE, and DELETE statements does not affect the return values of this API.

You can use SELECT LAST_INSERT_ID (); to query the return value of LAST_INSERT_ID.

INSERT multiple records using a single INSERT statement. LAST_INSERT_ID () only returns the AUTO_INCREMENT value generated by the first inserted record.

Article address:

Reprint at will ^ please include the address of this 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.