我有两个需要连接的数据库,可以在我编写的控制器和库中进行连接。出于某种奇怪的原因(我想我只是错过了一些简单的东西),我不能让它在模型中突然工作。我已经阅读了ci用户指南中的数据库类。
我试着在加载pew时引用$pew($this->pew=&$this->load->database('pew',true))但没有用。
有什么想法和建议吗?谢谢!
错误

PHP Fatal error:  Call to a member function query() on a non-object in
/Sites/CI/nyan/application/models/pewpewmodel.php on line 15

第15行
$this->pew->query('SELECT * FROM ExtractEvent'); //simplified for testing

数据库.php:
$active_group = 'nyan';
$active_record = TRUE;

$db['nyan']['hostname'] = 'catcatcat';
$db['nyan']['username'] = 'mew';
$db['nyan']['password'] = 'meow';
$db['nyan']['database'] = 'meow';
$db['nyan']['dbdriver'] = 'mysql';

$db['pew']['hostname'] = 'jujubees';
$db['pew']['username'] = 'qwop';
$db['pew']['password'] = 'qwop';
$db['pew']['database'] = 'nom';
$db['pew']['dbdriver'] = 'mssql';

pewpewmodel.php型号
private $pew;

function __construct()
{
    parent::__construct();
    $this->pew = $this->load->database('pew', TRUE);
}

function get_forms_by_date($id = NULL, $Year = NULL, $Month = NULL, $Day = NULL)
{
    $this->pew->query('SELECT * FROM ExtractEvent'); //simplified for testing
}

控制器nomnom.php
public function index()
{
    $this->load->model('pewmodel');
    $data['Forms'] = $this->pewmodel->get_forms_by_date($this->session->userdata('Username'), date('Y'), date('n'), date('j'));
    $this->load->view('common/header', $data['Forms']);
    $this->load->view('home/index');
    $this->load->view('common/footer');
}

查看index.php
<pre>
<?php print_r($Forms); ?>
</pre>

最佳答案

这对我起了作用。
[来自模型构造函数]

        $db['hostname'] = 'localhost';
        $db['username'] = 'root';
        $db['password'] = '';
        $db['database'] = 'my_database';
        $db['dbdriver'] = 'mysql';
        $db['dbprefix'] = '';
        $db['pconnect'] = TRUE;
        $db['db_debug'] = TRUE;
        $db['cache_on'] = FALSE;
        $db['cachedir'] = '';
        $db['char_set'] = 'latin1';
        $db['dbcollat'] = 'latin1_bin';
        $db['swap_pre'] = '';
        $db['autoinit'] = TRUE;
        $db['stricton'] = FALSE;

        $this->load->database($db, FALSE, TRUE);
        // False=don't return db object
        // True =use as active record, so it replaces default $this->db

希望这能像我一样帮助程序员通过谷歌搜索。

09-10 16:43