我很难将我的代码连接到我的 phpmyadmin 数据库。 我收到此错误:
"SQLSTATE[HY000] [1045] Access denied for user '//username'@'localhost' (using password: YES)"
我的数据库连接如下所示:
<?php
class Database {
private static $dbName = '//database name here' ;
private static $dbHost = 'localhost' ;
private static $dbUsername = '//username here';
private static $dbUserPassword = '//password here';
private static $cont = null;
public function __construct() {
die('Init function is not allowed');
}
public static function connect()
{
// One connection through whole application
if ( null == self::$cont )
{
try
{
self::$cont = new PDO( "mysql:host=".self::$dbHost.";"."dbname=".self::$dbName, self::$dbUsername, self::$dbUserPassword);
}
catch(PDOException $e)
{
die($e->getMessage());
}
}
return self::$cont;
}
public static function disconnect()
{
self::$cont = null;
}
}
?>
我想知道我的 localhost 和 codeanywhere 是否有问题,或者我只是输入了 phpmyadmin 错误。谢谢。
最佳答案
使用 mysql> select user,host from mysql.user;
命令检查您是否有 mysql 用户 'username'@'localhost'。如果您有一个主机为 127.0.0.1 的用户名条目,则在连接参数中将 localhost 替换为 127.0.0.1。或者添加 mysql 用户 'username'@'localhost' 并重试。
关于php - Codeanywhere 和 Phpmyadmin 数据库连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49870485/