如何使用自定义连接获取数据库内容

时间:2016-03-18 16:34:21

标签: codeigniter-3

我想每次都自动保存sql查询,我发现这个article,如果我按页面执行所有操作,但问题是,我有主数据库和奴隶数据库,因此我必须连接到数据库,如:

模型/ 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;
    }
}

然后我调整演示代码,如:

<?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);
    }
}

当然我收到了错误消息

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Users::$master

Filename: hooks/query_log.php

如何正确使用?

0 个答案:

没有答案