0
The connection to the bank is working perfectly, but the getList method returns me this error:
Fatal error: Uncaught Error: Call to a member function prepare() on null in /opt/lampp/htdocs/site/config/config.class.php:32 Stack trace: #0 /opt/lampp/htdocs/site/config/config.class.php(43): Conexao->getList() #1 {main} thrown in /opt/lampp/htdocs/site/config/config.class.php on line 32
i understood that this on line 32, but I can’t find anything wrong on that line, can anyone give me a help please? the code is below:
<?php
class Conexao {
private $sql,$user,$pass,$lista;
public function __construct($sql = 'mysql:host=localhost;dbname=teste',$user = 'root', $pass = '') {
$this->sql = $sql;
$this->user = $user;
$this->pass = $pass;
}
public function Conecta(){
try {
new PDO($this->sql,$this->user,$this->pass);
}catch(Exception $e) {
var_dump($e);
}
}
public function getList() {
$this->lista = $this->Conecta()->prepare('SELECT * FROM outros');
$this->lista->execute();
$this->lista->fetchAll();
}
}
$testando = new Conexao;
$testando->Conecta();
$testando->getList();
The method
Conecta
should not return the PDO instance?– Woss
What do you mean? I’m sorry but I don’t understand
– Otavio Fagundes
Within the method
getList
you access the methodprepare
than return fromConecta
, but such method returns nothing. Try to put:return new PDO(...)
– Woss
gave right, thank you very much, I thought the method Try made the return automatically, without having to send back something
– Otavio Fagundes