如何判断 MySQL UPDATE 何时成功与实际更新的数据?

How do I tell when a MySQL UPDATE was successful versus actually updated data?(如何判断 MySQL UPDATE 何时成功与实际更新的数据?)
本文介绍了如何判断 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 何时成功与实际更新的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)