Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 819
StackOverflowNewbie Avatar asked Oct 20 '11 23:10

StackOverflowNewbie


People also ask

Where can we see log messages and error messages in CodeIgniter?

By default, CodeIgniter displays all PHP errors. You might wish to change this behavior once your development is complete. You'll find the error_reporting() function located at the top of your main index.


1 Answers

Try these CI functions

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

UPDATE FOR CODEIGNITER 3

Functions are deprecated, use error() instead:

$this->db->error();  
like image 84
Oskenso Kashi Avatar answered Sep 27 '22 20:09

Oskenso Kashi