Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

codeigniter

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?

like image 253
MJ X Avatar asked Oct 20 '25 09:10

MJ X


2 Answers

use affected_rows();

 $this->db->affected_rows()

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

like image 97
bipen Avatar answered Oct 23 '25 20:10

bipen


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
}
like image 32
Haisum Usman Avatar answered Oct 23 '25 19:10

Haisum Usman