如何判断Mysql更新是否成功 [英] How to determine if Mysql update succeeded

查看:125
本文介绍了如何判断Mysql更新是否成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PDO PHP如何判断Mysql更新是否成功?

How to determine if Mysql update succeeded with PDO PHP?

检查受影响的行

$sth->rowCount();

这仅在更新的行发生更改时才有效,但如果行未更改怎么办??然后 $sth->rowCount() 将返回 0 即使更新成功...

This would only work if the updated row has changes, but what if the row is unchanged?? Then $sth->rowCount() would return 0 even if the update has succeeded...

推荐答案

执行此操作的正常方法是根据相同条件构建 COUNT 查询,并在 UPDATE 之前运行它.

The normal way to do this would be to construct a COUNT query on the same criteria, and run it before your UPDATE.

这可能会受到竞争条件的影响,具体取决于您是否在事务中运行,如果是,则取决于隔离级别.因此,真正的答案取决于您需要该号码的用途.

This could be subject to race conditions depending whether you're running in a transaction, and if so on the isolation level. So the real answer depends what you need the number for.

这篇关于如何判断Mysql更新是否成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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