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

Asked

Viewed 3,138 times

0

I’m making 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 shows the following error:

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

This is the excerpt from the 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 . ';';
  • Trying to run on a server?

1 answer

1


Exchange localhost for 127.0.0.1. And give the setAttribute. Documentation 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;
}  

Ai on the call make

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

**

Comment update.

** Let’s say your call is so validation.

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

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

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

This is the login method: at the beginning of the page do the include where you have the getConexao method();

  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;
            }
        }
  • This first code you sent was very helpful. But this second I put it in the call? Because if I put in the same file as the first one, it gives error.

  • Take a look here: https://github.com/gustinoco/Sislabweb/blob/master/entidade/Cliente.php This is a project of mine that I make use of this file, note that at the beginning I give the include da conf and use only getConexao()

  • There is a single file. My files are separate. Even my file that makes the connection is config.php. Inside my login.php file, I made the config include and put this second code. And still has error.

  • See if it helps you, in reply I made the edit.

Browser other questions tagged

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