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开发者_如何学Go, Rangana
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
}
In addition, you can also use $this->db->affected_rows()
to check if something was actually updated.
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.
use this
return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
精彩评论