CodeIgniter Forums

Full Version: How to get database stuff by using customized connection
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?