Codeigniter $ this-> db-> affected_rows () always returns 1

I am using codeigniter version 2.0.3. I am trying to get the number of rows affected after an update request using

$this->db->affected_rows 

It always returns 1, even if no row has been updated. I tried using

 mysql_affected_rows() 

and it returns -1 for the request to fail and 0 if the record has not been updated.

Edit included my code

I just use

 $country_id = $this->input->post('country_id'); $time=$this->input->post('time'); $insert_array = array( 'country' => $this->input->post('name') ); $this->db->update('country_master', $insert_array, array("country_id" => $country_id,"last_updated"=>$time)); $afftectedRows=$this->db->affected_rows(); 
+6
source share
1 answer

Actually my code was like that

 if (!$country_id) { $this->db->insert('country_master', $insert_array); $id = $this->db->insert_id(); } else { $this->db->update('country_master', $insert_array, array("country_id" => $country_id, "last_updated" => $time)); } $afftectedRows = $this->db->affected_rows(); 

And I changed it to

 if (!$country_id) { $this->db->insert('country_master', $insert_array); $id = $this->db->insert_id(); } else { $this->db->update('country_master', $insert_array, array("country_id" => $country_id, "last_updated" => $time)); $afftectedRows = $this->db->affected_rows(); } 

And now it works great.

And thanks for the answers.

+14
source

Source: https://habr.com/ru/post/904325/


All Articles