1

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_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';

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>

3 Answers 3

2

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.

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

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

6
  • i used two different names above when I pasted the question, whoops, fixed. Jul 11, 2011 at 18:04
  • What does var_dump($this->pew); say? Jul 11, 2011 at 20:27
  • For some reason, CI doesn't returns a database object. Maybe it can't connect or the group doesn't exist in the config file (maybe a typo). No error message? Nothing in the logs? Jul 12, 2011 at 8:46
  • php_error.log - [11-Jul-2011 10:29:54] PHP Fatal error: Call to a member function query() on a non-object in /Sites/CI/nyan/application/models/pewpewmodel.php on line 15 If I switch the name of the database group from pew to nyan (which it loads by default) the connection returns true and can connect to the DB no problem Jul 14, 2011 at 15:37
  • just tried using the DSN style $dsn = 'mssql://qwop:qwop@jujubees/nom'; $this->pew = $this->load->database($dsn); var_dump results: bool(false) in the constructor and the method. the connection string does work, just not from CI. I have been writing the same code in two separate projects, I'll check those results in the other project and see if it works there then edit this post. Jul 14, 2011 at 16:11
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.