php 函数last_insert_id() 不适用于 REPLACE INTO 查询

发布于 2024-08-28 19:18:26 字数 143 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江湖彼岸 2024-09-04 19:18:26

您可以尝试使用 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 as REPLACE 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.

寂寞清仓 2024-09-04 19:18:26

REPLACE INTO 似乎不会影响通过 last_insert_id() 获取的值。

从这个来看,这似乎是预期的行为:

REPLACE INTO doesn't seem to affect the vaue that can be obtained via last_insert_id().

It seems to be the expected behavior, judging from this :

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文