PDO Help Using MySQL

0

I am having trouble connecting to the database using PDO, I use MySQL.

When I run the connection to the database, the following error messages appear:

  

Fatal error: Uncaught exception 'PDOException' with message 'in C: \ wamp64 \ www \ phpCRUD \ config \ Conecta.php on line 11

     PDOException: in C: \ wamp64 \ www \ phpCRUD \ config \ Connect.php on line 11

I do not know where I'm going wrong, could anyone help me?

Here is the code for the bank configuration:

<?php
    require_once 'conf.php';
    class Conecta extends conf{
        var $pdo;
        function __construct(){
            $pdo = new PDO('mysql:host='.$this->host.':dbname='.$this->db, $this->usuario, $this->senha);
        }
        function login($email, $senha){
            $stmt = $this->PDO->prepare("SELECT * FROM usuarios WHERE email = :email AND senha = :senha");
            $stmt->bindValue(":email",$email);
            $stmt->bindValue(":senha", $senha);

            $exec = $stmt->execute();
            $run = $stmt->fetchAll(PDO::FETCH_ASSOC);
            return self::$run;
        }
    }

conf.php

<?php
    class conf{
        var $host = 'localhost';
        var $usuario = 'root';
        var $senha = '';
        var $db = 'hoo';
    }
    
asked by anonymous 24.11.2016 / 21:52

1 answer

1

You're making a little confusion with the $pdo attribute and the PDO class

When creating the $pdo attribute, do not use var, use this way:

private $pdo;

Now that you already have your attribute in the class, let's assign it an instance of the PDO class of php, so in the constructor do so:

$this->pdo = new PDO('mysql:host='.$this->host.':dbname='.$this->db, $this->usuario, $this->senha);
//Atributo $pdo recebe uma instância da classe PDO

The prepare this way:

$stmt = $this->pdo->prepare("SELECT * FROM usuarios WHERE email = :email AND senha = :senha")

In return also use this way, in case you want to return an associative array with all values of the query

return $stmt->fetchAll(PDO::FETCH_ASSOC);
    
24.11.2016 / 22:21