Extended logging of database errors in CodeIgniter

Some of you might and now and then have run into a database error while working with CI. Such errors are mostly caused by errors in your querys like not escaping user input or your fields or simply syntax errors.

In small applications that not much of a problem, but the bigger the application the harder it get’s to find the part where the bad query is hiding.

dootzky from the CodeIgniter Forum had exactly that problem and with a little help from wiredesignz we were able to add some more detail to the error messages!

Simply add this to the body of ‘application/errors/error_db.php’:

if($_SERVER['HTTP_HOST'] == 'localhost') {
   $trace = debug_backtrace();
   echo "The error occured in file ".$trace[4]['file']." on line ".$trace[4]['line']."";
}

Now additionally to the error you already know, you will be shown in which file and on which line the error occured (the db->get() method was called)! As this might pose a security risk on live systems, dootzky added the check for localhost.

I know that with CI 2.0 this might be obsolete because the error messages were enhanced, atleast the database errors now show filename and line, but someone still using some older version of CI (like I do for most of my older projects) might still find this handy.

P.S.: This is an older post I copied from my old blog. Thought some of you might find this useful.


Comments

3 responses to “Extended logging of database errors in CodeIgniter”

  1. Peter Bowen Avatar
    Peter Bowen

    Thanks David,

    Just what I was looking for in a CI project I’m working on.

    Cheers

    Pete

  2. Thomas Decaux Avatar
    Thomas Decaux

    You should never rely on $_SERVER variable since any body can alter it. Even HTTP_HOST can be “hacked”.

    1. Good point! Thanks.

      This post is “somewhat” out-dated.
      I doubt there are many out there still using CodeIgniter and running it on using WAMP/XAMP instead of a Virtual Machine.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.