3

I am using this function this codeigniter function after update query

$this->db->affected_rows();

it returns number of rows affected if i update values, but if i update values with the same as previous it retuns 0 rows affected

Any help...

h_a86
  • 281
  • 1
  • 8
  • 16
  • You get 0 rows affected the 2nd time you run the same query? I would think this was the right behavior since there is nothing to change when you run the same query for the 2nd time. What is the problem you're having? – Jay Sidri May 26 '12 at 12:08
  • yes i understand what i need is the confirmation that query has successfully run. – h_a86 May 26 '12 at 12:09

2 Answers2

7

yes i understand what i need is the confirmation that query has successfully run.

All CI database functions return a result if they "successfully run".

So

$result = $this->db->update('your_table', $data);
if ( ! $result)
{
     // Error
}
{
     // Function ran ok - do whatever
}
Laurence
  • 58,936
  • 21
  • 171
  • 212
0

In order to check that the database query succeeded, you can check the error code by using:

$this->db->_error_message();

If that is empty, you had no error. If not, you have the error message. _error_number() is also set, so you can use that instead if you are looking for a particular error.

Generally, you can skip the error check if you know there were affected_rows(), so you can limit the _error_message() test to situations where affected_rows() returns 0.

gaige
  • 17,263
  • 6
  • 57
  • 68