php – MySQLI 28000/1045用户’root’@’localhost’拒绝访问
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – MySQLI 28000/1045用户’root’@’localhost’拒绝访问,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2992字,纯文字阅读大概需要5分钟。
内容图文
![php – MySQLI 28000/1045用户’root’@’localhost’拒绝访问](/upload/InfoBanner/zyjiaocheng/889/2b8ee1ab6fb74a259906c87c30cbc275.jpg)
我遇到了一些麻烦.所以我试图使用MySQLi连接到我的数据库,但我收到此错误:
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.
<?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文件:
<?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:
<?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)
使用凭据定义一个数组,并且传递如下:
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变量而不是像我一样在本地定义它.但是在创建Users对象时必须将它传递给构造函数.
global.php中的错误
$users = new Users($database);
我想,一定是:
$users = new Users($db);
如果在该文件中定义了$db.
内容总结
以上是互联网集市为您收集整理的php – MySQLI 28000/1045用户’root’@’localhost’拒绝访问全部内容,希望文章能够帮你解决php – MySQLI 28000/1045用户’root’@’localhost’拒绝访问所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。