Error Call to a Member Function prepare

Asked

Viewed 3,372 times

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?

  • What do you mean? I’m sorry but I don’t understand

  • Within the method getList you access the method prepare than return from Conecta, but such method returns nothing. Try to put: return new PDO(...)

  • gave right, thank you very much, I thought the method Try made the return automatically, without having to send back something

1 answer

3


Missing one return inside Conecta, do so:

public function Conecta(){

    try {

        return new PDO($this->sql,$this->user,$this->pass);

    }catch(Exception $e) {
        var_dump($e);
    }

}

If the return is not null and will not have access to the methods of new PDO

In fact you don’t even need to call Connecta every time, you could create a variable to check if you have already instated the PDO class, like this:

<?php 

class Conexao {

    private $sql, $user, $pass, $lista, $pdo;

    public function __construct($sql = 'mysql:host=localhost;dbname=teste',$user = 'root', $pass = '') {

        $this->sql  = $sql;
        $this->user = $user;
        $this->pass = $pass;

    }

    public function Conecta(){

        //Verifica se já está conectado
        if ($this->pdo) {
            return $this->pdo;
        }

        try {

            $this->pdo = new PDO($this->sql,$this->user,$this->pass);

        }catch(Exception $e) {

            var_dump($e);

        }

        return $this->pdo;    
    }

And you don’t have to call this line:

$testando->Conecta();

Since you’re wearing it Conecta internally in class:

$testando = new Conexao;
$testando->getList();
  • gave right, thank you very much, I thought the method Try made the return automatically, without having to send back something

  • @Otaviofagundes recommend reading http://meta.pt.stackoverflow.com/a/1079/3635

Browser other questions tagged

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