Finding the number of affected rows can be useful in several ways—perhaps you want to update some records and only proceed if a certain number of records are updated, or perhaps you simply want to display the number of rows that have been deleted or updated by a query.
function update($id, $data) { $this->db->where('id', $id); if ($data->db->update($data, 'table_name')) { return $this->db->affected_rows(); } else { return false; } }
The model function update()
accepts two parameters: a $data
array and the $id
array of the database row we wish to update.
Next, we test for the returned value of $this->db->update($data);
, which will return true if successful and false if there was an error. If the returned value is true, we grab the number of affected rows for the update with the following line:
return $this->db->affected_rows();
If the update doesn't happen, the returned value will be false.
18.190.217.134