简体   繁体   中英

Codeigniter Fatal error: Call to a member function query() on a non-object

Now I am learning Codeigniter. I have more databases, so I choosed this time Anketa one. Why is error:

**Fatal error: Call to a member function query() on a non-object in /var/www/domains/svastara/application/controllers/anketa.php on line 12** ???

I chacked, the user and pass in database is ok, database is loaded in conf, and chacked the table name. So what more?

$this->db = $this->load->database('anketa');

        $q = $this->db->query("SELECT * FROM anketaip");

        if($q->num_rows()>0){
        foreach ($q->result() as $row)
        {
            $data[] = $row;
        }

        }return $data;

        $this->load->view('anketa_nova', $data);

Below line doesn't return you database object until you pass second argument as TRUE

Change:

$this->db = $this->load->database('anketa');

To

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

$q = $newdb->query("SELECT * FROM anketaip");

Reference Link: http://codeigniter.com/user_guide/database/connecting.html

You dont have to do this

$this->db = $this->load->database('anketa');

You can simply do it like this

$this->load->database('anketa');

$this->db->query('blah blah');

Also make sure database exists and you have rights to access it.

Posibly you need to load the DB library, please check in you autoload.php that you are loading the database library

$autoload['libraries'] = array('database');

Or load the library in your controller

$this->load->library('database');

Hi Daniela,

I think this resource link may help u. Because when you are using more than one database at a time u need to follow the syntax explained by GDB in his post. But you said unable to access to database that means it's not able to detect database,php in config folder. So put that file link externally in your php file. Then it may work.

http://codeigniter.com/forums/viewthread/191673/

That was the problem. I set $db['default']['autoinit'] = TRUE and it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM