1

我有两个需要连接的数据库,可以在我编写的控制器和库中完成。出于某种奇怪的原因(我假设我只是在这里遗漏了一些简单的东西)我突然无法让它在模型中工作。我已阅读 CI 用户指南中的数据库类。

我尝试在加载 pew ($this->pew =& $this->load->database('pew', TRUE)) 时引用 $pew 无济于事。

有什么想法、建议吗?谢谢!

错误

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>
4

3 回答 3

2

这对我有用。

[来自模型构造函数]

        $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

希望这有助于编码人员像我一样通过谷歌搜索。

于 2013-01-07T12:01:43.580 回答
1
$this->pew = $this->load->database('pew', TRUE);

You load a database which doesn't exist in your database-configuration.

于 2011-07-11T17:59:34.293 回答
0

当我通过 TRUE 进行如下连接时,出现 500 错误。我假设(是的,我知道)我只是在连接时遇到了同样的问题。说实话,从一开始就是 ID10T 错误。我缺少本地机器上的 mssql.so 库和另一个我正在测试的库。

$this->pew =& $this->load->database('pew', TRUE)

对于浪费时间的绅士们,我深表歉意。

于 2011-07-15T16:29:36.503 回答