PDO:使用mysql INSERT ON DUPLICATE KEY UPDATE检查更新或插入的记录

时间:2022-05-21 04:38:18

Using PDO / PHP and MySQL, how can I check if a record was inserted or updated when I use an INSERT ON DUPLICATE KEY UPDATE statement?

使用PDO / PHP和MySQL,当我使用INSERT ON DUPLICATE KEY UPDATE语句时,如何检查是否插入或更新了记录?

I have seen a solution using mysql_affected_rows() for PHP, but I am looking for a way I can use with PDO.

我已经看到了使用mysql_affected_rows()的PHP解决方案,但我正在寻找一种可以与PDO一起使用的方法。

1 个解决方案

#1


19  

If you use PDO::exec() the return value will be 1 if the row has been inserted and 2 if the row has been updated.
If you're using a prepared statement and PDOStatement::execute() the same is true for PDOStement::rowCount()

如果使用PDO :: exec(),则如果已插入行,则返回值为1;如果已更新行,则返回值为2。如果您正在使用预准备语句和PDOStatement :: execute(),则PDOStement :: rowCount()也是如此

#1


19  

If you use PDO::exec() the return value will be 1 if the row has been inserted and 2 if the row has been updated.
If you're using a prepared statement and PDOStatement::execute() the same is true for PDOStement::rowCount()

如果使用PDO :: exec(),则如果已插入行,则返回值为1;如果已更新行,则返回值为2。如果您正在使用预准备语句和PDOStatement :: execute(),则PDOStement :: rowCount()也是如此