phpcodeigniterexceptionerror-handling

CodeIgniter - how to catch DB errors?


Is there a way to make CI throw an exception when it encounters a DB error instead of displaying a message like:

A Database Error Occurred Error Number: 1054 Unknown column 'foo' in 'where clause' SELECT * FROM (FooBar) WHERE foo = '1'

NOTE: I only want this to happen in one controller. In the other controllers, I'm happy for it to display the DB error messages.


Solution

  • Use error() method:

    $this->db->error(); 
    

    For CodeIgniter 2, you can use the following functions which are now deprecated:

    $this->db->_error_message(); (mysql_error equivalent)
    $this->db->_error_number(); (mysql_errno equivalent)