Welcome Guest, Not a member yet? Register   Sign In
Undefined Variable when loading a database?
#1

[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'];
        }

    }

}
#2

[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'];
        }

    }

}
#3

[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.




Theme © iAndrew 2016 - Forum software by © MyBB