Codeigniter - handling errors when using active record

Whether you're using the active record class or not, you can access database errors using $this->db->_error_message() and $this->db->_error_number().

If you're using a mysql database, these functions are equivalent to mysql_error() and mysql_errno() respectively. You can check out these functions by looking at the source code for the database driver for the database you're using. They're located in system/database/drivers.

So, after you run a query, you can check for errors using something like:

if ($this->db->_error_message()) \\handle error

Straight from the CodeIgniter support forum:

$res = $this->db->query($str);

if (!$res) {
  // if query returns null
  $msg = $this->db->_error_message();
  $num = $this->db->_error_number();

  $data['msg'] = "Error(".$num.") ".$msg;
  $this->load->view('customers_edit_view',$data);
} 

Note that you can also log Active Record errors by going into your CI app config file and setting the following:

$config['log_threshold'] = 1;