Phpmysql_insert_id () returns the Latest Database id implementation method _ PHP Tutorial

Source: Internet
Author: User
Phpmysql_insert_id () returns the Latest Database id implementation method. Phpmysql_insert_id () returns the Latest Database id implementation method. for details, refer. The following code copies the code mysql_insert_id (). the function returns the INSERT php mysql_insert_id () in the previous step and returns the Latest Database id implementation method. for details, refer to it.
The code is 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 ().

The code is as follows:

$ Con= mysql_connect ("localhost", "hello", "321 ");
If (! $ Con)
{
Die ('could not connect: '. mysql_error ());
}

$ Db_selected = mysql_select_db ("test_db", $ con );

$ SQL = "INSERT INTO person VALUES ('cart', 'Thomas ', 'beijinging ')";
$ Result = mysql_query ($ SQL, $ con );
Echo "ID of last inserted record is:". mysql_insert_id ();

Mysql_close ($ con );
?>

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.

Http://www.bkjia.com/PHPjc/630741.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/630741.htmlTechArticlephp mysql_insert_id () returns the Latest Database id implementation method for students can refer. The code is as follows: the mysql_insert_id () function returns the previous INSERT...

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.