PHP returns the id_PHP tutorial generated by the insert operation

Source: Internet
Author: User
PHP returns the id generated by the insert operation. In some cases, you need to know the generated ID after executing the INSERT operation on the database (the primary key of MySQL is AUTO_INCREMENT, when executing the INSERT operation, you do not need to specify the ID value, but sometimes you need to know the generated ID after executing the INSERT operation of the database (in MySQL I use, the primary keys are set AUTO_INCREMENT, when you perform the insert operation, you do not need to specify the ID value and the database will generate it). In this case, you can use the PHP function mysql_insert_id () to directly obtain this ID, which is very convenient.

However, if the INSERT statement explicitly specifies the value of the ID column with the AUTO_INCREMENT attribute, the function returns 0 because the ID value is specified by the user, it is not automatically generated by the database.

The PHP manual describes the function as follows:

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. MySQL connection is required. If not specified, use the previous connection.

Mysql_insert_id () returns the AUTO_INCREMENT ID generated in the previous INSERT query in the given connection. If no connection is specified, the last 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 ().

 

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 last opened connection is used. If no AUTO_INCREMENT value is 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 for the first inserted record.

Values (in MySQL I use, the primary key is set AUTO_INCREMENT. when executing the insert operation, you do not need to specify the ID value instead...

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.