Codeigniter db->update() VS MySQL本机更新受影响的行:0。

时间:2022-10-06 12:33:15

Using MySQL alone - If I make a basic update to a table like this:

仅使用MySQL——如果我对这样的表进行基本更新:

UPDATE `SOMETABLE` SET `NAME` = 'John' WHERE `ID` = 1;

And the value of NAME = 'John' was already 'John' - in other-words - nothing is new, nothing to update. MySQL returns "Affected rows: 0 (Query took 0.0007 sec)"

而名字= 'John'的值已经是'John'——换句话说——没有什么是新的,没有什么是要更新的。MySQL返回“受影响的行:0(查询花费0.0007秒)”

If I make the same call - now using CodeIgniter - and then retrieve the affected rows like this:

如果我进行同样的调用——现在使用CodeIgniter——然后检索受影响的行,如下所示:

$data = array(
               'NAME' => 'John'
            );

$this->db->where('ID', 1);
$this->db->update('SOMETABLE', $data); 
$affect = $this->db->affected_rows();

echo $affect; // $affect echos 1

$affect ends up equaling 1. I haven't got a problem with this - I just expected that if there was nothing to update - that codeigniter would behave the same way as MySQL and not edit something that does not need to be updated, and return 0 for affected_rows().

$affect等同于1。我没有遇到这个问题——我只是希望如果没有什么需要更新的东西——codeigniter的行为与MySQL相同,不会编辑不需要更新的内容,并且对于affected_rows()返回0。

  • Have I got this wrong some way?
  • 我是不是弄错了?
  • Is codeigniter overwriting 'John'? or not?
  • 是codeigniter覆盖“约翰”?或不呢?

1 个解决方案

#1


3  

Try getting the query that CodeIgniter is running using the following code:

尝试使用以下代码获取CodeIgniter正在运行的查询:

$this->db->last_query();

Also post the query you are using to interact with MySQL, just to confirm that the exact same query is being run.

还可以发布与MySQL交互的查询,以确认正在运行完全相同的查询。

CodeIgniter does have a hack for MySQL that adjusts the reporting of affected rows, however I was under the impression it was only for DELETE queries. If you look at system/database/drivers/mysql/mysql_driver.php or system/database/drivers/mysqli/mysqli_driver.php (whichever driver you are using and look at the variable var $delete_hack = TRUE;. Adjusting that might impact your result, could be worth a try?

CodeIgniter确实有一个针对MySQL的hack,可以调整受影响行的报告,但是我认为它只是用于删除查询。如果您查看系统/数据库/驱动程序/mysql/mysql_driver的话。php或系统/数据库/司机/ mysqli / mysqli_driver。php(无论使用哪个驱动程序,请查看变量var $delete_hack = TRUE;调整可能会影响你的结果,值得一试吗?

#1


3  

Try getting the query that CodeIgniter is running using the following code:

尝试使用以下代码获取CodeIgniter正在运行的查询:

$this->db->last_query();

Also post the query you are using to interact with MySQL, just to confirm that the exact same query is being run.

还可以发布与MySQL交互的查询,以确认正在运行完全相同的查询。

CodeIgniter does have a hack for MySQL that adjusts the reporting of affected rows, however I was under the impression it was only for DELETE queries. If you look at system/database/drivers/mysql/mysql_driver.php or system/database/drivers/mysqli/mysqli_driver.php (whichever driver you are using and look at the variable var $delete_hack = TRUE;. Adjusting that might impact your result, could be worth a try?

CodeIgniter确实有一个针对MySQL的hack,可以调整受影响行的报告,但是我认为它只是用于删除查询。如果您查看系统/数据库/驱动程序/mysql/mysql_driver的话。php或系统/数据库/司机/ mysqli / mysqli_driver。php(无论使用哪个驱动程序,请查看变量var $delete_hack = TRUE;调整可能会影响你的结果,值得一试吗?