MJ X
MJ X

Reputation: 9044

How to check if a row is affected with codeigniter active record

I search through the net but i didn't find any solution, My problem is that how do I now during update that if a row values has changed or not or if a row is affected?

Upvotes: 4

Views: 8340

Answers (3)

Mohammad Naim Dahee
Mohammad Naim Dahee

Reputation: 939

I think this is the simplest way to achieve this goal.

return ($this->db->affected_rows() != 1) ? false : true;

Upvotes: 0

Haisum Usman
Haisum Usman

Reputation: 516

When we are working with CodeIgniter, the data is only updated when there is some change in the input field's value and then the $this->db->affected_rows() will return a value greater than 0.

Suppose we have two fields, 'name' and 'email'. If we try to submit the form without changing any of the field, then $this->db->affected_rows() will return 0, else it will return 1.

A better approach is to use:

if ($this->db->affected_rows() >= 0) {
    return true; // your code
} else {
    return false: // your code
}

Upvotes: 3

bipen
bipen

Reputation: 36531

use affected_rows();

 $this->db->affected_rows()

Displays the number of affected rows, when doing "write" type queries (insert, update, etc.).

Upvotes: 8

Related Questions