Error while trying to select with PHPOO

1

Well, I'm a beginner in PHPOO and I've developed a class with functions to connect to the database (I've already succeeded) and another to do a select (where the error occurs). I'll post the code and then report the error.

index.php:

<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
    require_once("class/Banco.class.php");

    $banco = new Banco("localhost", "phpoo", "root", "");

    $banco->Select();
}
?>

<!DOCTYPE html>
<html lang="pt-BR">
    <head>
        <meta charset="UTF-8">
        <title>Login com PHPOO</title>
    </head>

    <body>
        <form action="" method="POST">
            <input type="text" name="usuario" placeholder="Usuario">
            <input type="password" name="senha" placeholder="Senha">

            <input type="submit" value="Logar!">
        </form>
    </body>
</html>

Bank.class.php:

<?php
class Banco {
    protected $host;
    protected $bd;
    protected $usuario;
    protected $senha;

    public function __construct($host, $bd, $usuario, $senha) {
        $this->host = $host;
        $this->bd = $bd;
        $this->usuario = $usuario;
        $this->senha = $senha;

        try {
            $conexao = new PDO("mysql:host=".$this->host.";dbname=".$this->bd."", $this->usuario, $this->senha,
                array(
                    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, 
                    PDO::ATTR_PERSISTENT => false,
                    PDO::ATTR_EMULATE_PREPARES => false,
                    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
                )
            );
        } catch(PDOException $e) {
            echo "Ocorreu um erro: ".$e->getMessage();
        }
    }

    public function Select() {
        $listarUsuarios = $conexao->prepare("SELECT * FROM usuarios ORDER BY id DESC");
        $listarUsuarios->execute();

        $linhaUsuario = $listarUsuarios->fetchAll(PDO::FETCH_ASSOC);
        $contaUsuario = $listarUsuarios->rowCount();

        if($contaUsuario) {
            echo "Tem usuario: ".$contaUsuario;
        } else {
            echo "Nao tem usuario: ".$contaUsuario;
        }
    }
}
?>

When attempting to login, the following error is displayed:

Notice: Undefined variable: conexao in C:\xampp\htdocs\PHPOO\class\Banco.class.php on line 29

Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\PHPOO\class\Banco.class.php on line 29

As I mentioned earlier, I'm a beginner in PHPOO, so how can I solve both of these errors? And the way I'm making the connection is correct and secure?

Thanks!

    
asked by anonymous 18.03.2015 / 22:05

1 answer

1

The error is saying: Your $ connection variable does not exist. You instantiated it in the constructor, but in the select method it does not exist. You can do the following:

In class declaration:

protected $conexao;

In the constructor:

$this->conexao = new PDO("mysql:host=".$this->host.";dbname=".$this->bd."", $this->usuario, $this->senha,
            array(
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, 
                PDO::ATTR_PERSISTENT => false,
                PDO::ATTR_EMULATE_PREPARES => false,
                PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
            )
        );

In the Select method:

$listarUsuarios = $this->conexao->prepare("SELECT * FROM usuarios ORDER BY id DESC");

As for the way and the security of connecting, it is to say that it is correct and relatively safe. Others may contribute more to this.

I hope to have helped.

[] s

    
18.03.2015 / 22:12