MYSQL Tips--last_insert_id_php skills

Source: Internet
Author: User
In fact, these two are different, last_insert_id () can return the bigint value of the ID. Instead, MYSQL_INSERT_ID returns an int. If you
The ID is unsigned int, or bigint. Well, it may be wrong to return it. Instead, use LAST_INSERT_ID () instead.

Some friends, returned are 0, do not know how, in fact, last_insert_id () returned is the Auto_increment ID.
If, in the table structure, there is no setting, auto_increment cannot return.

Still others, or return to 0. Then you need to check if the Insert Delay function is used. In this case, the immediate return ID value is not returned.

A lot of people like to use SELECT Max (ID) ... To replace this last_insert_id, in fact, the Select MAX (ID) is not thread-safe, and it is quite possible that
The other thread inserts the new data and you can't find the ID you inserted last time. and last_insert_id is corresponding to a MySQL connect, which is
Corresponds to your current line threads relative and will not be interfered with by other threads. If you have some strange mistakes in your database, for example, to update the information on a data,
The result B data is updated, and is sometimes correct, sometimes incorrect, and most people are very incorrect. We need to see if we use the Select Max (ID).

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.