SQLSTATE[HY000] [1044] Access denied

Asked

Viewed 348 times

-1

Good evening, I am implementing my connection class with phpmyadmin, but I am receiving a denied access message, I have already checked the user, password and access permissions and it is correct. Could someone take a look at my code to see what might be wrong?

<?php

class Conexao {
    private $host = null;
    private $banco = null;
    private $usuario = null;
    private $senha = null;
    public $conn = null;
    public $sql = null;
    public $result = null;

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

    public function __get($atributo) {
        return $this->atributo;
    }

    public function __set($atributo, $valor) {
        $this->$atributo = $valor;
    }

    public function conexaoComBanco() {

        try {
            $this->conn = new PDO("mysql:host=$this->host; dbname=$this->banco, '$this->usuario', '$this->senha'");
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            echo 'Conexão realizada com sucesso!';

        } catch(PDOException $e) {
            echo $e->getMessage();
        }
    }
}

$conexao = new Conexao('localhost', 'bancoteste', 'wilder', '12345');

$conexao->conexaoComBanco();

?>

1 answer

1


You are creating a PDO instance in the wrong way, check the official documentation.

The correct is:

new PDO($dsn, $user, $password);

For example:

$this->conn = new PDO(
  'mysql:host=localhost;dbname=mydb',
  'root',
  'root'
);

Browser other questions tagged

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