问题描述
如何判断 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 何时成功与实际更新的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!