Home > Database > Mysql Tutorial > Mysql插入记录后返回该记录ID

Mysql插入记录后返回该记录ID

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 16:20:53
Original
1169 people have browsed it

最近和Sobin在做一个精品课程的项目,因为用到一个固定的id作为表间关联,所以在前一个表插入数据后要把插入数据生成的自增id传递给下一个表。研究了一番决定使用Mysql提供了一个LAST_INSERT_ID()的函数。 liehuo.net LAST_INSERT_ID() (with no argument) r

   最近和Sobin在做一个精品课程的项目,因为用到一个固定的id作为表间关联,所以在前一个表插入数据后要把插入数据生成的自增id传递给下一个表。研究了一番决定使用Mysql提供了一个LAST_INSERT_ID()的函数。 liehuo.net

LAST_INSERT_ID() (with no argument) returns the first automatically generated value that was set for an AUTO_INCREMENT column by the most recently executed INSERT or UPDATE statement to affect such a column. For example, after inserting a row that generates an AUTO_INCREMENT value, you can get the value like this:

Copy to Clipboard

  mysql> SELECT LAST_INSERT_ID();

  -> 195

  简单说来,,就是这个函数将返回插入的那条记录在表中自增的那个字段的值,一般我们都给那个自增字段命名为ID。这样就可以返回刚插入的记录的ID值了。 liehuo.net

  一个简单的例子:

  Copy to Clipboard

  $query="INSERT INTO `testtable` (`clou1`,`clou2`) VALUES ('testvalue','test')";

  mysql_query($query);

  $query="SELECT LAST_INSERT_ID()";

  $result=mysql_query($query);

  $rows=mysql_fetch_row($result);

  echo $rows[0];

  这个函数是基于connection的,也就是不会被其他客户端的connection影响到,所以结果是准确的。如果使用select max(id) from table,在高密度的插入请求下,是有可能出问题的,返回错误值

Related labels:
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
Latest Issues
MySQL stops process
From 1970-01-01 08:00:00
0
0
0
Error when installing mysql on linux
From 1970-01-01 08:00:00
0
0
0
phpstudy cannot start mysql?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template