MySQLI 28000/1045 用户“root"@“localhost"的访问被拒绝

MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用户“root@“localhost的访问被拒绝)
本文介绍了MySQLI 28000/1045 用户“root"@“localhost"的访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我遇到了一些麻烦.因此,我尝试使用 MySQLi 连接到我的数据库,但出现此错误:

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

.我不知道为什么它说用户为root",我的代码如下.我可以使用该信息登录 PHPMyAdmin.

. 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;
    }
}
?>

这是我的 global.php 文件:

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);
?>

这是我的用户类:

<?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');
        }
    }
}
?>

这里是routing.php:

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();
?>

推荐答案

错误原因在这里:

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

您没有将参数传递给 Users.__construct($db)

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;
    }

或者使用全局 $db 变量而不是像我一样在本地定义它.但是你必须在创建用户对象时将它传递给构造函数.

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.

$users = new Users($database);

我想,应该是:

$users = new Users($db);

如果在该文件中定义了 $db.

If $db is defined in that file.

这篇关于MySQLI 28000/1045 用户“root"@“localhost"的访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)