php 函数last_insert_id() 不适用于 REPLACE INTO 查询
我正在使用 REPLACE INTO 查询插入到表中,但是在使用 mysql_query() 函数执行查询后,如果我使用 last_insert_id() 它只会给我 0 值。
为什么会出现这样的情况呢?我怎样才能克服这种行为。
:潘卡杰
I am using REPLACE INTO query to insert in to table but after executing query by using mysql_query() function and if I use last_insert_id() it is only giving me 0 value.
why this is happening so? and how can I overcome this behavior.
:Pankaj
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用
INSERT INTO ... ON DUPLICATE KEY UPDATE
代替。它完成与 REPLACE INTO 相同的事情,但在单个服务器端操作中。REPLACE INTO
最终可能会导致两种操作:删除旧操作,插入新操作。但无论查询类型如何,您都必须确保表的主键是
auto_increment
字段。否则,last_insert_id() 将无法正常工作。You could try using
INSERT INTO ... ON DUPLICATE KEY UPDATE
instead. It accomplishes the same thing asREPLACE INTO
, but in a single server-side operation.REPLACE INTO
can end up causing two operations: delete the old one, insert the new one.But regardless of the query type, you do have to ensure that the table's primary key is an
auto_increment
field. last_insert_id() does not work properly otherwise.REPLACE INTO
似乎不会影响通过last_insert_id()
获取的值。从这个来看,这似乎是预期的行为:
LAST_INSERT_ID() 在
REPLACE INTO
查询后给出错误的值LAST_INSERT_ID()
REPLACE INTO
doesn't seem to affect the vaue that can be obtained vialast_insert_id()
.It seems to be the expected behavior, judging from this :
LAST_INSERT_ID()
give wrong value afterREPLACE INTO
queryLAST_INSERT_ID()