php 警告: mysqli_connect(): (HY000/1045): 用户 'username'@'localhost' 访问被拒绝(使用密码:YES)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25174183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning: mysqli_connect(): (HY000/1045): Access denied for user 'username'@'localhost' (using password: YES)
提问by
Warning: mysqli_connect(): (HY000/1045): Access denied for user 'username'@'localhost' (using password: YES) in C:\Users\xampp\htdocs\PHP_Login_Script\config.php on line 6
Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, boolean given in C:\Users\xampp\htdocs\PHP_Login_Script\login.php on line 10
Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, boolean given in C:\Users\xampp\htdocs\PHP_Login_Script\login.php on line 11
Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in C:\Users\xampp\htdocs\PHP_Login_Script\login.php on line 15
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\Users\xampp\htdocs\PHP_Login_Script\login.php on line 16
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\Users\xampp\htdocs\PHP_Login_Script\login.php on line 19
警告:mysqli_connect(): (HY000/1045): Access denied for user 'username'@'localhost' (using password: YES) in C:\Users\xampp\htdocs\PHP_Login_Script\config.php on line 6
警告:mysqli_real_escape_string() 期望参数 1 是 mysqli,布尔值在 C:\Users\xampp\htdocs\PHP_Login_Script\login.php 第 10 行给出
警告:mysqli_real_escape_string() 期望参数 1 是 mysqli,布尔值在 C:\Users\xampp\htdocs\PHP_Login_Script\login.php 第 11 行给出
警告:mysqli_query() 期望参数 1 是 mysqli,布尔值在 C:\Users\xampp\htdocs\PHP_Login_Script\login.php 第 15 行给出
警告:mysqli_fetch_array() 期望参数 1 为 mysqli_result,第 16 行 C:\Users\xampp\htdocs\PHP_Login_Script\login.php 中给出的 null
警告:mysqli_num_rows() 期望参数 1 为 mysqli_result,第 19 行 C:\Users\xampp\htdocs\PHP_Login_Script\login.php 中给出的 null
I'm getting this error above on localhost even if my config file is like this:
即使我的配置文件是这样的,我也会在本地主机上收到此错误:
<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_DATABASE", "databasename");
$db = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
?>
This used to work, but now it doesn't anymore. Is there any problem with this code or is it not working now? Thisis the tutorial I am working on right now.
这曾经有效,但现在不再有效。这段代码有什么问题还是现在不起作用?这是我现在正在研究的教程。
采纳答案by Ignacio Vazquez-Abrams
That combination of username, host, and password is not allowed to connect to the server. Verify the permission tables (reloading grants if required) on the server and that you're connecting to the correct server.
不允许用户名、主机和密码的组合连接到服务器。验证服务器上的权限表(如果需要,重新加载授权)并且您正在连接到正确的服务器。
回答by hooligan
If youre running wamp update
如果您正在运行 wamp 更新
define("DB_HOST", "localhost");
To your machines ip address (mine is 192.168.0.25);
到你机器的ip地址(我的是192.168.0.25);
define("DB_HOST", "192.168.0.25");
You can find it on window by typing ipconfig in your console or ifconfig on mac/linux
您可以通过在控制台中输入 ipconfig 或在 mac/linux 上输入 ifconfig 在窗口中找到它
回答by moez
mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
use DB_HOST instead of DB_SERVER
使用 DB_HOST 而不是 DB_SERVER
回答by Sathya
Make sure that your password doesn't have special characters and just keep a plain password (for ex: 12345), it will work. This is the strangest thing that I have ever seen. I spent about 2 hours to figure this out.
确保您的密码没有特殊字符,只需保留一个普通密码(例如:12345),它就会起作用。这是我见过的最奇怪的事情。我花了大约 2 个小时来解决这个问题。
Note: 12345 mentioned below is your plain password
注意:下面提到的 12345 是您的普通密码
GRANT ALL PRIVILEGES ON dbname.* TO 'yourusername'@'%' IDENTIFIED BY '12345';
回答by hemanth kumar
Look at your code You defined DB_HOST and querying DB_SERVER. DB_USER and DB_USERNAME Use this code
看看你的代码你定义了 DB_HOST 并查询 DB_SERVER。DB_USER 和 DB_USERNAME 使用此代码
define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_DATABASE", "databasename");
$connect = mysqli_connect(DB_SERVER , DB_USER, DB_PASSWORD, DB_DATABASE);
回答by headersalreadysent
you defIne as DB_USER but use as DB_USERNAME. Also php says username@localhost cant access. Not root@localhost.
您定义为 DB_USER 但用作 DB_USERNAME。php 还说 username@localhost 无法访问。不是 root@localhost。
Change your define or connect paramater.
更改您的定义或连接参数。
回答by Morris
I have fixed a few things here, the "DB_HOST" defined here should be also DB_HOST down there, and the "DB_USER" is called "DB_USER" down there too, check the code always that those are the same.
我在这里修复了一些问题,这里定义的“DB_HOST”也应该是 DB_HOST,而“DB_USER”在那里也被称为“DB_USER”,请始终检查代码是否相同。
<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_DATABASE", "");
$db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
?>
回答by Ahmed Bahtity
The same issue faced me with XAMPP 7 and opencart Arabic 3. Replacing localhost by the actual machine name reslove the issue.
我在 XAMPP 7 和 opencart Arab 3 中遇到了同样的问题。用实际的机器名称替换 localhost 解决了这个问题。
回答by Precious Tom
The question has been answered above, Just to make you feel comfortable using all four string at the same time in a connection
上面已经回答了这个问题,只是为了让您在连接中同时使用所有四个字符串时感到舒服
<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_DATABASE", "databasename");
$db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
// you could test connection eventually using a if and else conditional statement,
// feel free to take out the code below once you see Connected!
if ($db) {
echo "Connected!";
} else {
echo "Connection Failed";
}
?>
回答by Victor-Andrei Stanculescu
This is your code:
这是你的代码:
<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_DATABASE", "databasename");
$db = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
?>
The only error that causes this message is that:
导致此消息的唯一错误是:
- you're defining a
DB_USER
but you're calling after asDB_USERNAME
.
- 您正在定义 a
DB_USER
但您正在调用 asDB_USERNAME
。
Please be more careful next time.
下次请多加小心。
It is better for an entry-level programmer that wants to start coding in PHP
not to use what he or she does not know very well.
对于想要开始编码的入门级程序员来说,最好PHP
不要使用他或她不太了解的东西。
ONLY as advice, please try to use (for the first time) code more ubiquitous.
仅作为建议,请尝试使用(第一次)更普遍的代码。
ex: do not use the define()
statement, try to use variables declaration as $db_user = 'root';
例如:不要使用define()
语句,尝试使用变量声明作为$db_user = 'root';
Have a nice experience :)
有一个很好的体验:)