开发者

Switching databases in code igniter

I have two databases I need to connect to, which I can do in the controllers and libraries I have written. For some odd reason (i'm assuming I'm just missing something simple here) I can't get it to work in the model all of the sudden. I have read the database class in the CI user guide.

I tried making a reference to $pew when loading pew ($this->pew =& $this->load->database('pew', TRUE)) to no avail.

Any thoughts, suggestions? Thanks!

Error

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

Line 15

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

database.php:

$active_group = 'nyan';
$active_rec开发者_如何学Goord = 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';

Model 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
}

Controller 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');
}

View index.php

<pre>
<?php print_r($Forms); ?>
</pre>


This worked for me.

[from within the model constructor]

        $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

Hope this helps coders searching through Google like I did.


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

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


I was getting a 500 error when I passed TRUE to connect like below. I assumed (yeah I know) that I was just having the same problem with connecting. truth be told it was an ID10T error from the beginning. I was missing the mssql.so library on my local machine and another I was testing from.

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

My apologies for the waste of time gents.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜