Query log in codeigniter using hook

后端 未结 2 535
南笙
南笙 2021-01-16 10:31

I need to create a query log in my project. So I created a post_controller hook. It saves all the executed queries in both a text file and a database. But it wo

相关标签:
2条回答
  • 2021-01-16 11:24

    You need to check your internal redirection after any modification query(Insert, Update or delete query) executed. If you put any redirect statement after modification query then it will overtake hook execution.

    You can do it by overwriting the query() method in system/database/DB_driver.php

    Or

    Create library and call it from relevant controllers.

    0 讨论(0)
  • 2021-01-16 11:28

    My code skipping all queries other than SELECT because of internal redirection. So I created a library for this. I am attaching my code here. It may help someone else

    application/libraries/Querylog.php

    class Querylog {
        protected $CI;
    
        public function __construct() {        
            $this->CI =& get_instance();
        }
    
        function save_query_in_db() {
            $query = $this->CI->db->last_query();
            $times = $this->CI->db->query_times; 
            $time = round(doubleval($times[2]), 5);
            $this->CI->db->query('INSERT INTO queryLog_tbl(`query`, `executedTime`, `timeTaken`, `executedBy`) '
                . 'VALUES ("'.$query.'", "'.date('Y-m-d h:i:s').'", "'.$time.'","'.$this->CI->session->userdata('UserID').'")');
        }
    }
    

    load this library in your controller or autoload.php

    and call save_query_in_db() where ever you want

    eg: in model :

    $this->db->set('status', 1);
    $this->db->where('UserID', $this->session->userdata('UserID'));
    $this->db->update('user_tbl');
    $this->querylog->save_query_in_db();
    
    0 讨论(0)
提交回复
热议问题