php function last_insert_id() is not working with REPLACE INTO query
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 afterREPLACE INTO
queryLAST_INSERT_ID()
精彩评论