MySQLI 28000/1045 Access denied for user 'root'@'localhost'

前端 未结 2 767
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-22 10:48

I am having some trouble. So I am trying to connect to my database using MySQLi, but I am getting this error:

Warning: mysqli::mysqli(): (28000/1045): Acces         


        
相关标签:
2条回答
  • 2020-12-22 10:58

    Usually this means that one of your host, database, user or password is wrong.

    In my case it was the host, I usually use localhost but this time I saw in my cPanel > MySQL® Databases that I should not use localhost and istead I should use mysql5.xxxxxxx.net.

    This was the message that helped me:

    The mysql server’s address is mysql5.xxxxxxxx.net. When connecting to the mysql server you must specify this host.

    0 讨论(0)
  • 2020-12-22 11:05

    The reason of the error is here:

    class Routing {
        public function __construct($route) {
            $this->users = new Users();//<-?
            $this->route = $route;
        }
    

    You are not passing a parameter to Users.__construct($db)

    Define an array with credentials and pass is like this:

    class Routing {
        public function __construct($route) {
            $db = array();
            $db['host'] = 'localhost';
            $db['user'] = 'venge_main';
            $db['pass'] = 'fakepassword';
            $db['name'] = 'venge_panel';
    
            $this->users = new Users($db);
            $this->route = $route;
        }
    

    Or use a global $db variable instead of defining it locally, like I did. But you must pass it to constructor when creating Users object.

    Error in global.php

    $users = new Users($database);
    

    I think, it must be:

    $users = new Users($db);
    

    If $db is defined in that file.

    0 讨论(0)
提交回复
热议问题