CodeIgniter: SQL audit of all calls $ this-> db-> query ()?

I am using CodeIgniter 2+ and would like to audit all calls to $this->db->query($sql); .

All calls to our database are made by the query () method; no active use of entries. I need to write $ sql queries and enter them into a user table for audit purposes. Is there any way to extend the base database library for query auditing?

It seems to be easy, but I cannot find a simple solution. There are several error messages on older versions of the CI forum.

+4
source share
3 answers

It depends on how you want to check them. If you are searching for each page, then enabling the profiler will be fine. This shows all the requests that are executed when this page loads, as well as the time taken to complete them. See the link below on the profiler.

http://codeigniter.com/user_guide/general/profiling.html

If you want to log all queries as they arise, and then read the log file later, you will have to extend the database class. If so, please comment and I will update / extend my answer further.

Extension to rewrite query()

Extend MY_Loader.php in / application / core / and paste this function

 function database($params = '', $return = FALSE, $active_record = NULL) { // Grab the super object $CI =& get_instance(); // Do we even need to load the database class? if (class_exists('CI_DB') AND $return == FALSE AND $active_record == NULL AND isset($CI->db) AND is_object($CI->db)) { return FALSE; } require_once(BASEPATH.'database/DB'.EXT); // Load the DB class $db =& DB($params, $active_record); $my_driver = config_item('subclass_prefix').'DB_'.$db->dbdriver.'_driver'; $my_driver_file = APPPATH.'core/'.$my_driver.EXT; if (file_exists($my_driver_file)) { require_once($my_driver_file); $db = new $my_driver(get_object_vars($db)); } if ($return === TRUE) { return $db; } // Initialize the db variable. Needed to prevent // reference errors with some configurations $CI->db = ''; $CI->db = $db; } 

Then create / application / core / MY _DB_mysql_driver.php

Then inside you can rewrite the request ()

 function query($sql, $binds = FALSE, $return_object = TRUE) { // Do your stuff return parent::query( $sql, $binds, $return_object ); } 

Obviously, replace mysql in the file name with any database driver that you are using / trying to expand.

This will also work with Active Record, since all get() methods call query() from the driver to launch their queries.

+5
source

If you just want to do it directly ...

The query function is located in the line system / database / DB_driver.php 250 (or so depending on your version).

Here's a comment that says โ€œRun the queryโ€ on about line 289.

Print $sql any way you like at this point.

+3
source

It seems that you can extend the database class and overwrite the query method, but according to Phil , this is not possible.

One solution is to create a helper method that you can replace with all your calls to $this->db->query so that it will log your queries and then call the query method.

UPDATE

You can create a library that copies the _compile_queries library's _compile_queries method (line 168 https://github.com/EllisLab/CodeIgniter/blob/develop/system/libraries/Profiler.php ). Usually this method displays all requests to the browser, but you can implement it in such a way as to register all requests. then not only will you receive the queries generated with $this->db->query , but all the queries are executed.

+3
source

All Articles