关于获取上一步insert的SQL语句的id的问题

WBOY
Release: 2016-06-23 13:46:04
Original
1123 people have browsed it

用mysql_insert_id函数的话,当错作流程是:A页面插入了一条,id=1,B页面插入了一条,id=2,A页面执行mysql_insert_id获取到的id是1还是2?


回复讨论(解决方案)

A页面当然是 1

A页面当然是 1


我可以理解为是返回`当前页面`的上一步执行的inser语句的id吗,w3school上说得不太清楚

如果不是1 ,就麻烦了,那么写所有的东西,都要操心并发的问题了。

可以对mysql服务器发送查询

mysql_query("select  LAST_INSERT_ID()");
Copy after login
 如果是一万个页面呢?再如果是重要的事务性操作呢?查询数据库比较靠谱

A页面的mysql_insert_id返回是1

MySQL的mysql_insert_id是针对MySQL进程来进行的,对于服务器来说mysql_query都是一个原子操作,不是MAX(id),就算在大并发的情况下也不会出现不准确的情况。

可以看看MySQL的

mysql_insert_id是这么定义的

my_ulonglong STDCALL mysql_insert_id(MYSQL *mysql)
{
  return mysql->;last_used_con->;insert_id;
}

MYSQL参数是个结构体,里面包括数据库链接和一些当前数据库链接的状态值,

其中在MYSQL结构体里面有insert_id,mysql_insert_id函数返回的就是结构体里面的找个值。

肯定是1,自己可以实际操作一下,就明白了

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!