Error query php / codeigniter

-2

I am using the code below to query the database, but it is giving the following error:

  

PHP Error was encountered   Severity: Notice   Message: Undefined property: Admin :: $ db   Filename: core / Model.php   Line Number: 51

     

Fatal error: Call to a member function query () on a non-object in /home/*/public_html/application/models/admin_model.php on line 4 **

Code

class Admin_model extends CI_Model {
   public function carregar_hoteis(){
        $hoteis = $this->db->query("SELECT * FROM tabela");
        $array=array();
        foreach($hoteis->result() as $row)
        {
             $array['hoteis'][]=$row;
        }
        echo json_encode($array);
   }
}
    
asked by anonymous 10.03.2014 / 21:33

2 answers

3

The problem is in the $this->db variable, which is not an object. Probably this variable receives an instance of the connection to the database, the connection should not be happening and therefore the value of the variable is null (not an object).

Tip: Check if the framework is connecting to the database and if, at the time of that code, you tried to make a connection:)

    
10.03.2014 / 21:45
2

Did you initialize the Database library in the application / config / autoload.php file?

$autoload['libraries'] = array('database');
    
20.05.2014 / 23:01