2

I am using REPLACE INTO command for inserting/updating record in MySQL table. However I want to get ID of row which gets affected by REPLACE INTO.

I used lastInsertId() with INSERT INTO but it is not useful over here because if my record already exists then by using lastInsertId() with REPLACE INTO, i'll get wrong ID.

So is there any function present in MySQL + php + PDO to get that id?

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Bharat Jain
  • 29
  • 1
  • 5
  • Can't you be more certain please? Providing a test case and the code to reproduce? It's quite hard to verify whatever results you may get or misinterpret, without the code. Thanks. – Your Common Sense Jul 26 '13 at 14:00

1 Answers1

1

Since REPLACE INTO does a DELETE and then an INSERT you'll always get the "wrong" id with any lastInsertId() function (wrong as in "not the one you're probably expecting").

So you either have to live with that, look into INSERT INTO ... ON DUPLICATE KEY UPDATE or specify what you are trying to accomplish so we can give further advice on how to proceed.

mgrueter
  • 1,400
  • 6
  • 15
  • here more info http://mikefenwick.com/blog/insert-into-database-or-return-id-of-duplicate-row-in-mysql/ – jaczes Jul 26 '13 at 14:00
  • Thanks for your help. I just wanted to check is there any function available like lastInsertID() for Replace INTO or not. – Bharat Jain Jul 26 '13 at 14:21