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

25,185

Solution 1

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

Solution 2

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.

Solution 3

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');
Share:
25,185
Daniela
Author by

Daniela

Updated on July 09, 2022

Comments

  • Daniela
    Daniela almost 2 years

    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);
    
  • Daniela
    Daniela over 11 years
    If I do that i have an Error: Unable to connect to your database server using the provided settings.
  • Daniela
    Daniela over 11 years
    I read somewhere if there is more databases with same user and pass its better to put FALSE if I want them to work.
  • GBD
    GBD over 11 years
    can you paste your database.php data ?
  • Daniela
    Daniela over 11 years
    I SOLWED IT :) THANKS GUYS FOR HELP!:)
  • Daniela
    Daniela over 11 years
    I SOLWED IT :) THANKS GUYS FOR HELP!:)
  • m4t1t0
    m4t1t0 over 11 years
    Please, say what is the problem
  • Daniela
    Daniela over 11 years
    I SOLWED IT :) THANKS GUYS FOR HELP!:)
  • Daniela
    Daniela over 11 years
    First time that what you wrote here, after, i copied one pass in DB conf, And I forgot to change the database group name.
  • Daniela
    Daniela over 11 years
    First time that what GBD wrote, after, i copied one pass in DB conf, And I forgot to change the database group name.
  • Venkata Krishna
    Venkata Krishna over 11 years
    how u solved daniela.......whats the error if my post helped you accept it as answer..