Welcome Guest, Not a member yet? Register   Sign In
db behaviour problem
#1

[eluser]Unknown[/eluser]
Hi, i'm experimenting a frustrating problem, probably the behaviour it's right, if so PLEASE specify it in the user guide, thank you.

The problem is that if i don't load database's class in my controller's constructor, i can't point my db with "$this->db" and i get that silly error
"Undefined property: MyController::$db";

Why autoload function don't put the "db" in the global scope ($this->db) ?
How can i avoid this behaviour ?

this work:
Code:
//mycontroller.php
function MyController()
{
    parent::Controller();
    $this->load->database();
}



this don't work:
Code:
//mycontroller.php
function myfunction()
{
    $this->load->database();
    $query = $this->db->query('SELECT * FROM mytable');
    foreach ($query->result() as $row)
    {
        echo $row->field1;
        echo $row->field2;
    }
    echo 'Total Results: ' . $query->num_rows();
}



and this don't work too:
Code:
//autoload.php
$autoload['libraries'] = array('database');

//mycontroller.php
function myfunction()
{
    $query = $this->db->query('SELECT * FROM mytable');
    foreach ($query->result() as $row)
    {
        echo $row->field1;
        echo $row->field2;
    }
    echo 'Total Results: ' . $query->num_rows();
}





thankyou very much, and sorry for my bad speach Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB