Error: SQLSTATE [HY000] [1044] Access denied for user 'root' @ 'localhost' to database 'login'

0

I'm doing a login system, using phpmyadmin for the database. My code was developed by PhpStorm and the files are being sent by FileZilla. When trying to run the code, it has the following error:

  

Error: SQLSTATE [HY000] [1044] Access denied for user 'cnsmaxi' @ 'localhost' to database 'login'

This is the snippet of code:

// Variáveis da conexão
$base_dados  = 'login';
$usuario_bd  = 'cnsmaxi';
$senha_bd    = '52045b2943';
$host_db     = 'localhost';
$charset_db  = 'UTF8';
$conexao_pdo = null;

// Concatenação das variáveis para detalhes da classe PDO
$detalhes_pdo  = 'mysql:host=' . $host_db . ';';
$detalhes_pdo .= 'dbname='. $base_dados . ';';
$detalhes_pdo .= 'charset=' . $charset_db . ';';
    
asked by anonymous 20.07.2016 / 20:29

1 answer

1

Change the localhost to 127.0.0.1. And give setAttribute. Documentation pdo Create in a separate file the connection.php Give the include on every page that uses it.

    define("PORT", "3306");
    define("DB", "login");
    define("END", "127.0.0.1");
    define("USER", "root");
    define("PASS", "123");    
  function getConexao() {
    $conn = new PDO('mysql:host=' . END . ';port=' . PORT . ';dbname=' . DB . ';charset=utf8', USER, PASS);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    return $conn;
}  

Already on call

$conn = getConexao();
$sql = "SELECT * FROM login";
$result = $conn->prepare($sql);
$result->execute();
$usuario = $result->fetch(PDO::FETCH_ASSOC);

**

Comments update.

** Let's say your call is validated this way.

$retorno =  login($_POST['Usuario'], $_POST['Senha']);

When accessing the login (user, password) it will return a boolean. Then do it below the call.

if($retorno) {
     echo 'Fez login';
else 
   echo 'Usuario ou senha invalidos';

Login method is this: at the beginning of the page do the include where it has the method getConexao ();

  function login($login, $senha) {
            $login = mysql_escape_string($login);
            $senha = mysql_escape_string($senha);
            try {
                $sql = "SELECT usuario FROM login WHERE Login='$login' AND Senha='$senha' LIMIT 1";
                $conn = getConexao();
                $result = $conn->query($sql);
                $row = $result->fetch(PDO::FETCH_OBJ);
                 //faça um print_r no $row para fazer o retorno...
                return true;
            } catch (PDOException $e) {
                return false;
            }
        }
    
20.07.2016 / 21:03