Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

codeigniter

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();
like image 273
Nick Avatar asked Dec 22 '11 15:12

Nick


1 Answers

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

like image 127
Nick Avatar answered Oct 06 '22 08:10

Nick