views:

119

answers:

2

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

+2  A: 

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 :

Pascal MARTIN
That applies if you specify the value for the auto-increment field (i.e. turning off the magic ;-)). Is that the case with your queries, PankajK?
VolkerK
A: 

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.

Marc B