getting mysql_insert_id() while using ON DUPLICATE KEY UPDATE with PHP

后端 未结 4 1428
南旧
南旧 2020-12-05 02:52

I\'ve found a few answers for this using mySQL alone, but I was hoping someone could show me a way to get the ID of the last inserted or updated row of a mysql DB when using

4条回答
  •  生来不讨喜
    2020-12-05 03:37

    Although not using mysql_insert_id() and ON DUPLICATE KEY UPDATE, alternative great way to get the value of any field when updating another found here:

    UPDATE table SET id=(@tempid:=id) , .... LIMIT 1;
    SELECT @tempid;
    

    I used it having table with (id,status) 'id' primary index auto-increment, and 'status' was the field upon which update was made, but i needed to get 'id' of the updated row. This solution also proof to race conditions as mysql_insert_id().

提交回复
热议问题