A
A
alan312021-07-03 16:36:46
PHP
alan31, 2021-07-03 16:36:46

Why can't I connect to the database?

For some reason, I can’t connect to the database either using PDO or using mysqli, I use OpenServer, for some reason it pops up:

Error!: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES)

If anything, here is my php code:
$user = 'root';
$password='';
$host='localhost';
$db='site';
$charset='utf8';

$opt = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];

$req = "mysql:host=$host;dbname=$db;charset=$charset";

try {
  $pdo = new PDO($req,$user,$password);    
} catch (PDOException $e){
    print "Error!: " . $e->getMessage() . "<br/>";
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Satisfied IT, 2021-07-03
specialist @borisdenis

Access denied for user 'root'@'localhost' (using password: YES)

This usually indicates that the password for the root user is not correct. Judging by your config - the password is not specified at all. Check your credentials.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question