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

Asked

Viewed 335 times

0

I made a class for connection to the database using PDO and at the time I will 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'];
}

He of the error in the foreach line

  • 1

    This error happens when there is some error in sql, what I noticed was that the from is pasted with the table name. Try to print the query and test directly in the database.

  • I used the 'var_dump' function to find out the error, I had forgotten to put information, in the PDO object.

  • 2

    Can you add the solution in more detail in response form in the field below? So content can help future visitors to the site. Thank you!

  • The error may be a wrong syntax in sql

1 answer

1


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

Initially she would return to me false. So I went to check the line where I put the object PDO, I saw that I had forgotten the = on the part of host

Before

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

Afterward

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.