简体   繁体   中英

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

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): Access denied for user 'root'@'localhost' (using password: NO) in /home/venge/public_html/library/classes/database.class.php on line 16

Warning: Missing argument 1 for Users::__construct(), called in /home/venge/public_html/routing.php on line 4 and defined in /home/venge/public_html/library/classes/users.class.php on line 3

Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'root'@'localhost' (using password: NO) in /home/venge/public_html/library/classes/database.class.php on line 16

Warning: Cannot modify header information - headers already sent by (output started at /home/venge/public_html/library/classes/database.class.php:16) in /home/venge/public_html/routing.php on line 11

. I have no idea why it says "root" as the user, my code is below. I can login to PHPMyAdmin with that info fine.

<?php

$db['host'] = 'localhost';
$db['user'] = 'venge_main';
$db['pass'] = 'fakepassword';
$db['name'] = 'venge_panel';


class DB {
    function __construct($db) {
        $this->mysqli = new mysqli($db['host'], $db['user'], $db['pass'], $db['name']);
    }
    function query($i) {
        return $this->mysqli->query($i);
    }
    function fetch_array($i) {
        return $i->fetch_array(MYSQLI_ASSOC);
    }
    function num($i) {
        return $i->num_rows;
    }
}
?>

Here is my global.php file:

<?php
session_start();

$venge['library'] = 'library/classes/';

include_once($venge['library'] . 'users.class.php');
include_once($venge['library'] . 'database.class.php');
$users = new Users($database);
?>

Here is my users class:

<?php
class Users {
    public function __construct($db) {
        $this->db = new DB($db);
    }
    public function uidset() {
        if (isset($_SESSION['uid'])) {
            return true;
        } else {
            return false;
        }
    }
    public function securitycheck() {
        if (isset($_SESSION['uid'])) {
            //passed
            return true;
        } else {
            //failed
            die('No permissions');
        }
    }
}
?>

Here is routing.php:

<?php
class Routing {
    public function __construct($route) {
        $this->users = new Users();
        $this->route = $route;
    }
    public function File() {
        if (!$this->users->uidset()) {
            switch ($this->route) {
                default:
                    header("Location: /user/login");
                    break;
                case '/user/login':
                    include_once('library/pages/login.page.php');
                    break;
            }
        } else {
            switch ($this->route) {
                default:
                    header("Location: /venge");
                    break;
                case '/venge':
                    echo 'Welcome to <strong>Venge</strong> .';
                    break;
            }
        }
    }
}

$route = new Routing($_SERVER['ORIG_PATH_INFO']);

$route->File();
?>

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.

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM