1

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

Pankaj Khairnar
  • 3,028
  • 3
  • 25
  • 34

2 Answers2

4

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
  • 356,200
  • 43
  • 426
  • 500
3

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
  • 395,085
  • 80
  • 655
  • 663
  • 1
    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 Apr 15 '10 at 11:19