mysqli::insert_id 在重复键上返回错误的 ID
当在 PHP 中使用 mysqli::insert_id 进行 INSERT INTO ON DUPLICATE KEY UPDATE 时,如果更新了行,我会不断获取下一个自动增量而不是更新的行。在同一个数据库但在另一个表中,使用 ON DUPLICATE KEY UPDATE 时没有这种行为。相反,我获取更新行的 ID。但现在由于某种原因,在我创建的新表中,我不断获得下一个甚至不存在的 id。两个表都是MyISAM,并且有一个自动增量字段。我不明白为什么他们的行为不同。
示例:
==================================
Table: example
==================================
id | unique_field | data
==================================
1 | unique1 | 123
2 | unique2 | 456
INSERT INTO
example
SET
unique_field = 'unique1',
data = '321'
ON DUPLICATE KEY UPDATE
data = '321'
// mysqli::insert_id returns 3 (not 1)!!
有什么想法吗?
When using mysqli::insert_id in PHP for INSERT INTO ON DUPLICATE KEY UPDATE, I keep getting the next auto increment rather than the updated row if the row is updated. In the same database but in another table, I don't have this behavior when using ON DUPLICATE KEY UPDATE. Instead, I get the id of the updated row. But now for some reason in a new table I have created, I keep getting the next id that doesn't even exist. Both tables are MyISAM, and have an auto increment field. I don't understand why they behave differently.
Example:
==================================
Table: example
==================================
id | unique_field | data
==================================
1 | unique1 | 123
2 | unique2 | 456
INSERT INTO
example
SET
unique_field = 'unique1',
data = '321'
ON DUPLICATE KEY UPDATE
data = '321'
// mysqli::insert_id returns 3 (not 1)!!
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 mysql 手册:
希望有帮助。
From the mysql manual:
Hope that helps.