简体   繁体   English

mysql_insert_id进行查询?

[英]mysql_insert_id for a query?

I am trying to add a record to a mysql database and retrieve that records id immediately after. 我试图将一条记录添加到mysql数据库中,然后立即检索该记录id。 I am looking to use mysql_insert_id() however I am wondering does it pull the very last record or the last record you have inserted? 我正在寻找使用mysql_insert_id()的方法,但是我想知道它会拉出最后一条记录还是您插入的最后一条记录?

What i'm thinking if two users of the website both run querys then the last record added may not be that users record, so does mysql_insert_id() differentiate between the two or just pull the last record? 我在想什么,如果网站的两个用户都运行查询,那么最后添加的记录可能不是该用户记录,那么mysql_insert_id()区分两者还是仅提取最后一条记录?

Thanks. 谢谢。

very last record or the last record you have inserted? 最后一条记录还是您插入的最后一条记录?

a latter one. 后一个。 it is clearly stated on this function's manual page 在此功能的手册页上有明确说明

You do not need to worry about 2 users colliding as mysql_insert_id() will return the auto_increment ID of the last record inserted on that connection resource. 您不必担心用户碰撞为mysql_insert_id()将返回插入连接资源的最后一条记录的AUTO_INCREMENT ID。 The other user should have a different MySQL connection. 另一个用户应具有不同的MySQL连接。

So, if you have... 所以,如果你有...

$r = mysql_query("INSERT INTO foo VALUES('bar')");
$id = mysql_insert_id()

You'll get the ID from that INSERT statement. 您将从 INSERT语句获取ID。

If you have multiple connections in the one script, just make sure you're passing the optional link identifier to your MySQL calls. 如果在一个脚本中有多个连接,只需确保将可选的链接标识符传递给MySQL调用即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM