views:

66

answers:

3

Hi Guys,

i'm using these two lines to update my table using codeigninter active records

$this->db->where('reference_number', $reference);
$this->db->update('patient', $data); 

what i want to de is to check weather it successfully updates the table and according to that i want to give a notification to the user, how can i check the successful update is happened with this lines? there is a no clue in the user guide telling that by putting the line

if($this->db->update('patient', $data));

will give us a true or false value, can we do like that? or is there any other solution to this problem?

regards, Rangana

+2  A: 

You can put a code like this in your model...

function func() {
    $this->db->where('reference_number', $reference);
    $this->db->update('patient', $data); 

    $report = array();
    $report['error'] = $this->db->_error_number();
    $report['message'] = $this->db->_error_message();
    return $report;
}

_error_number and _error_message use the mysql_errno and mysql_error functions of php.

Then inside your controller, you can check for the error like this...

$this->load->model("Model_name");
$report = $this->Model_name->func();
if (!$report['error']) {
  // update successful
} else {
  // update failed
}
ShiVik
thank you very much ShiVik! it was helpful!
ranganaMIT
+2  A: 

In addition, you can also use $this->db->affected_rows() to check if something was actually updated.

Bogdan
thank you very much Bogdan for this additional detail, i will implement that also! :)
ranganaMIT
A: 

Can I just follow on from @ShiVik - if you use the _error_number() or _error_message() functions, you will need to disable automatic database error reporting.

You can do this in /config/database.php. Set db_debug = FALSE.

The way I handle this functionality in my system is just to test the result returned by the ActiveRecord class using something like

if($this->db->update('patient', $data) === TRUE) {   
  $flash_data = array('type' => 'success', 'message'
=> 'Update successful!');
$this->session->set_flashdata('flash', $flash_data);
}

Although usage of the flash session data system is up to you :) You'd need to add in the relevant front-end to this in your view files, and tweak to your liking etc.

George