Questions about getting the ID of the SQL statement for the previous insert

Source: Internet
Author: User
Questions about getting the ID of the SQL statement for the previous insert
With the mysql_insert_id function, when the wrong process is: a page inserted a, id=1,b page inserted a bar, id=2,a page execution mysql_insert_id get the ID is 1 or 2?
------to solve the idea----------------------
A page of course is 1
------to solve the idea----------------------
If it is not 1, it will be troublesome, then write all the things, you have to worry about the problem of concurrency.
------to solve the idea----------------------
Queries can be sent to the MySQL server
mysql_query ("Select  last_insert_id ()");
What if it's a 10,000 page? And what if it's an important transactional operation? Query the database more reliable
------to solve the idea----------------------
A mysql_insert_id Return of page A is 1

MySQL mysql_insert_id is for the MySQL process, for the server, mysql_query is an atomic operation, not Max (ID), even in the case of large concurrency does not appear inaccurate.

Can look at the MySQL

MYSQL_INSERT_ID is so defined.

My_ulonglong stdcall mysql_insert_id (MySQL *mysql)
{
Return mysql->;last_used_con->;insert_id;
}

The MySQL parameter is a struct that includes the database link and some status values for the current database link.

Where the insert_id,mysql_insert_id function inside the MySQL struct returns the value found inside the struct.
------to solve the idea----------------------
Must be 1, I can actually operate it, I understand
  • 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.