Reputation: 1869
I'm using codeigniter version 2.0.3. I'm trying get the number of affected rows after an update query using
$this->db->affected_rows
It always returns 1 even if no row had been updated. I tried with
mysql_affected_rows()
and it returns -1 for a query failure and 0 if no record had been updated .
Edit included my code
I'm just using
$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();
Upvotes: 16
Views: 90994
Reputation: 1869
Actually my code was like this
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 modified 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 its working fine now.
And thank you very much for the replies..
Upvotes: 27