CodeIgniter Forums
How to get database stuff by using customized connection - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: How to get database stuff by using customized connection (/showthread.php?tid=64659)



How to get database stuff by using customized connection - chan15 - 03-18-2016

I want to auto save the sql query every time, I found this article, if I do everything by the page it will work, but the problem is, I have master and slave database, therefore I have to connect to database like:

model/Users.php



PHP Code:
I want to auto save the sql query every timeI found this article, if do everything by the page it will workbut the problem isI have master and slave databasetherefore I have to connect to database like:

model/Users.php

<?php

class Users extends CI_Model
{
 
   protected $table 'users';

 
   public $master;
 
   public $slave;

 
   public function __construct()
 
   {
 
       $this->master $this->load->database('master'true);
 
       $this->slave $this->load->database('slave'true);
 
   }

 
   public function save($datas)
 
   {
 
       $this->master->insert($this->table$datas);

 
       return $this->master;
 
   }



Then I adjust demo code like:


PHP Code:
<?php

class Query_log
{
 
   public function log_query()
 
   {
 
       $ci =& get_instance();

 
       $filepath APPPATH 'logs/Query-log-' date('Y-m-d') . '.php';
 
       $handle fopen($filepath"a+");

 
       $times $ci->master->query_times;

 
       foreach ($ci->master->queries as $key => $query) {
 
           $sql $query " \n Execution Time:" $times[$key];
 
           fwrite($handle$sql "\n\n");
 
       }

 
       fclose($handle);
 
   }


Of course I got error message


Quote:
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Users::$master

Filename: hooks/query_log.php

How to make it right?