0

我很难将我的代码连接到我的 phpmyadmin 数据库。我收到此错误:

“SQLSTATE [HY000] [1045] 用户'//用户名'@'localhost'的访问被拒绝(使用密码: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。谢谢你。

4

1 回答 1

0

mysql> select user,host from mysql.user;检查您是否有一个带有命令的mysql用户'username'@'localhost' 。如果您有一个作为用户名的条目,主机为 127.0.0.1,则在连接参数中将 localhost 替换为 127.0.0.1。或者添加一个 mysql 用户 'username'@'localhost' 并重试。

于 2018-04-17T07:28:25.467 回答