开发者

php function last_insert_id() is not working with REPLACE INTO query

开发者 https://www.devze.com 2022-12-27 12:59 出处:网络
I am using REPLACE INTO query to ins开发者_Go百科ert 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.

I am using REPLACE INTO query to ins开发者_Go百科ert 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


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.


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 :

  • LAST_INSERT_ID() give wrong value after REPLACE INTO query
  • LAST_INSERT_ID()
0

精彩评论

暂无评论...
验证码 换一张
取 消