当查询中传递的数据与数据库中已经存在的数据相同时,如何确定MySQL更新查询是否成功? [英] How to determine if a MySQL update query succeeded when the data passed in the query is the same as what is already in the database?

查看:140
本文介绍了当查询中传递的数据与数据库中已经存在的数据相同时,如何确定MySQL更新查询是否成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您的数据库中包含预填充数据的表单,并允许用户进行更改并保存表单。如果用户单击保存按钮而不进行任何更改,MySQL将不会实际执行写入操作,因此affected_rows将返回0。



我了解行为,但是确定更新是否失败的最佳做法是检查受影响的人数吗?



区分实际失败的更新的最佳做法是什么?一个成功但是导致0个受影响的人,以便我可以向用户提供反馈?

解决方案

只需检查是否执行查询后发生错误。

如果您使用 mysql ,请检查 mysql_error()

if(!mysql_error())print'all is fine';

mysqli


Let's say you have a form with pre-populated data from your database, and you allow your users to make changes and save the form. If the user clicks the save button without making any changes, MySQL will not actually perform a write operation, and therefore the affected_rows will return 0.

I understand the behavior, but what is the best practice for determining if an update failed, other than checking for the number of affected_rows?

What is the best practice for differentiating between an update that actually failed, and one that "succeeded" but resulted in 0 affected_rows so that I can provide feedback to the user?

解决方案

Just check if no errors occurred after execution of query.
If you use mysql, check mysql_error():
if (!mysql_error()) print 'all is fine';
Same for mysqli.

这篇关于当查询中传递的数据与数据库中已经存在的数据相同时,如何确定MySQL更新查询是否成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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