How do I tell when a MySQL UPDATE was successful versus actually updated data?(如何判断 MySQL UPDATE 何时成功与实际更新的数据?)
问题描述
如何判断 MySQL UPDATE 何时成功与实际更新的数据?
How do I tell when a MySQL UPDATE was successful versus actually updated data?
示例:
TABLE
id city_name
1 Union
2 Marthasville
如果我运行以下命令:
$data = array('city_name', 'Marthasville');
//update record 2 from Marthasville to the same thing, Marthasville.
$this->db->where('id', 2);
$this->db->update('table', $data);
if($this->db->affected_rows() > 0)
{
//I need it to return TRUE when the MySQL was successful even if nothing was actually updated.
return TRUE;
}else{
return FALSE;
}
这将在每次 UPDATE 语句成功时返回 TRUE
,但在没有实际更新行时返回 FALSE.
This will return TRUE
every time the UPDATE statement is successful, but FALSE when no rows were actually updated.
我需要它在每次成功执行 UPDATE 语句时返回 TRUE
,即使它实际上并没有更改任何记录.
I need it to return TRUE
every time the UPDATE statement was successfully executed even if it doesn't actually change any records.
推荐答案
看看mysql_affected_rows()
它应该告诉您是否实际更新了任何内容,而不是没有成功更新任何内容并返回 true.
It should tell you if anything was actually updated as opposed to nothing was successfully updated resulting in a return of true.
php.net 说:
mysql_affected_rows()
mysql_affected_rows()
成功时返回受影响的行数,如果最后一个则返回 -1查询失败.
Returns the number of affected rows on success, and -1 if the last query failed.
您可以使用以下方法来实现您想要的结果:
You could use the following to achieve your desired results:
if($this->db->affected_rows() >= 0){ }
这篇关于如何判断 MySQL UPDATE 何时成功与实际更新的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何判断 MySQL UPDATE 何时成功与实际更新的数据?


基础教程推荐
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01