PDO:使用mysql INSERT ON DUPLICATE KEY UPDATE检查更新或插入的记录 [英] PDO: check for updated or inserted record using mysql INSERT ON DUPLICATE KEY UPDATE

查看:60
本文介绍了PDO:使用mysql INSERT ON DUPLICATE KEY UPDATE检查更新或插入的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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

推荐答案

如果使用 PDO :: exec ()如果已插入行,则返回值为1;如果已更新行,则返回值为2.

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.

如果您使用准备好的语句和PDOStatement :: execute(),则 PDOStatement也是如此:: rowCount()

If you're using a prepared statement and PDOStatement::execute() the same is true for PDOStatement::rowCount()

这篇关于PDO:使用mysql INSERT ON DUPLICATE KEY UPDATE检查更新或插入的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆