CodeIgniter Forums
Undefined Variable when loading a database? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Undefined Variable when loading a database? (/thread-14430.html)



Undefined Variable when loading a database? - El Forum - 01-02-2009

[eluser]Solarpitch[/eluser]
Hey,

When I try to call the test function in the below model, I get the error...

Severity: Notice
Message: Undefined variable: callan_db
Filename: models/report_model.php

Dont really understand considering the variable is defined and I've set up the callan database connection settings correctly.

Code:
class Report_model extends Model {


    function Report_model()
    {
        parent::Model();
        $callan_db = $this->load->database('callan', TRUE);
    }
    
    function test()
    {

        $query = $callan_db->query("SELECT * FROM golfpro_barsales");
        
        if ($query->num_rows() > 0)
        {
           $row = $query->row_array();
        
           echo $row['year'];
        }

    }

}



Undefined Variable when loading a database? - El Forum - 01-02-2009

[eluser]Armchair Samurai[/eluser]
You haven't defined $callan_db for the entire class, just the constructor. You'll need to do this:

Code:
class Report_model extends Model {

    var $callan_db;

    function Report_model()
    {
        parent::Model();
        $this->callan_db = $this->load->database('callan', TRUE);
    }
    
    function test()
    {

        $query = $this->callan_db->query("SELECT * FROM golfpro_barsales");
        
        if ($query->num_rows() > 0)
        {
           $row = $query->row_array();
        
           echo $row['year'];
        }

    }

}



Undefined Variable when loading a database? - El Forum - 01-02-2009

[eluser]Solarpitch[/eluser]
I see... I tried...

$callan_db = ""; but that didnt work. I wasnt aware you needed to include $this-> before the variable either.