couldn't access database with error message:
SQLSTATE[28000] [1045] Access denied for user 'root’@‘xxx.ne.jp'(using password: YES)
I have this php code.
$dsn = 'mysql:dbname=mydb;host=xxx.ne.jp';
$user = 'root';
$password ='0123';
try{
$dbh = new PDO($dsn, $db_user, $db_password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo $e->getMessage();
}
I can login to phpmyadmin with same username and password(root/0123).
why?
Do you have any idea to fix it?
Here is the basic PDO connection example:
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
So include the missing host name :
$dsn = 'mysql:host=localhost;dbname=mydb;host=xxx.ne.jp';
$user = 'root';
$password ='0123';
try{
$dbh = new PDO($dsn, $db_user, $db_password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo $e->getMessage();
}
Change localhost
if running site on another host.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.