mysqlcodeigniterrows-affected

Codeigniter db->update() VS MySQL native UPDATE Affected rows: 0


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

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)"

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

$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().


Solution

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

    $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.

    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?