MYSQL tips -- LAST_INSERT_ID_PHP tutorial

WBOY
Release: 2016-07-21 15:43:03
Original
1278 people have browsed it

In fact, there is a difference between the two. LAST_INSERT_ID() can return the id of a bigint value. However, mysql_insert_id returns int. If your
id is unsigned int, or bigint. So, maybe what is returned is wrong. Use LAST_INSERT_ID() instead.

Some friends also return 0. I don’t know why. In fact, LAST_INSERT_ID() returns the ID of AUTO_INCREMENT.
If AUTO_INCREMENT is not set in the table structure, it cannot be returned.

There are still some people who still return 0. Then you need to check whether the insert delay function is used. In this case, the immediate return id value will not be returned.

Many people like to use select max(id) ... to replace this last_insert_id. In fact, select max(id) is not thread-safe. It is very likely that other threads have inserted new data. , you will not be able to find the ID you inserted last time. And last_insert_id corresponds to a mysql connect, that is,
corresponds to your current thread and will not be interfered by other threads. If some strange errors occur in your database, for example, the information of data A is supposed to be updated,
As a result, data B is updated, and sometimes it is correct and sometimes it is incorrect. When there are many people, it will be very is incorrect. Just check to see if select max(id) is used

http://www.bkjia.com/PHPjc/320813.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320813.htmlTechArticleActually, there is a difference between the two. LAST_INSERT_ID() can return the id of a bigint value. However, mysql_insert_id returns int. If your id is unsigned int, or bigint. So...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!