What causes "Call to a member function fetchAll () on a non-object in ..."?

0

I made a class to connect to the database using PDO and at the time I get the data from my table it gives this error.

This is the code:

require_once('../class/connection.class.php');

$db = conection::getInstancia();
$query = $db->query("SELECT * FROM". conection::getTabela('TB_ARQUIVOS'));

foreach($query->fetchAll(\PDO::FETCH_ASSOC) as $arquivo){
    echo $arquivo['Codigo'];
}

It's the error in the foreach line

    
asked by anonymous 10.06.2014 / 17:15

1 answer

1

I used the var_dump () function in the $ query variable.

Initially it would return me false . I then checked the line where I instantiated the PDO object, I saw that I had forgotten the = part of the host

Before

self::$instancia = new \PDO(self::$tipo_bd.':host'.self::$host.';dbname='.self::$nome_bd, self::$usuario, self::$senha);

Then

self::$instancia = new \PDO(self::$tipo_bd.':host='.self::$host.';dbname='.self::$nome_bd, self::$usuario, self::$senha);
    
10.06.2014 / 17:42