How to get the primary key value AutoNumber for insert data after MySQL INSERT statement

Source: Internet
Author: User
Tags mysql insert mysql tutorial

How do I get the primary key value AutoNumber for the insert data after the MySQL tutorial insert statement?

The method is very simple, the MySQL data with the mysql_insert_id ();

How to use:

Insert INTO (a ') VALUES (' B ')

$nid = mysql_insert_id ();

Method Two:

LAST_INSERT_ID (), but there is a lot of difference about this function compared to mysql_insert_id (), mysql_insert_id () is the insert_id that gets the current session directly, and last_insert_id () is a SQL function,
Needs to be obtained by executing SQL, such as SELECT last_insert_id (); Obviously the two effects are the same,
But it takes a lot of resources to execute SQL once, and it's much faster to get the value in memory directly via MYSQL_INSERT_ID ()

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.