简体   繁体   English

PHP 脚本无法连接到 MYSQL 数据库

[英]PHP script cannot connect to MYSQL database

I have a straight forward script that is trying to connect to a MySQL database.我有一个直接的脚本试图连接到 MySQL 数据库。

When I run the script, I get the error:当我运行脚本时,出现错误:

Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES) in C:\\xampp\\htdocs\\ShopSite\\submitReg.php on line 16 Failed to connect.警告: mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES) in C:\\xampp\\htdocs\\ShopSite\\submitReg.php on line 16 连接失败。

The connection code is:连接代码是:

$server="127.0.0.1";
$db="shop";
$user="root";
$password="";

$conn=mysqli_connect($server,$user,$password,$db) or die("Failed to connect");

I can connect to phpMyAdmin and edit the database on there just fine.我可以连接到 phpMyAdmin 并在那里编辑数据库就好了。 I am using XAMPP and running it locally.我正在使用 XAMPP 并在本地运行它。

When I go to user accounts, it says there is no password for root.当我转到用户帐户时,它说 root 没有密码。 I looked in the config file and there are no passwords set.我查看了配置文件,没有设置密码。 I haven't set a password for anything.我没有为任何东西设置密码。

I haven't went in and fiddled with settings, or created any new accounts.我没有进入并摆弄设置,也没有创建任何新帐户。 It was working fine (never used to get this problem), then it just started with this.它工作正常(从来没有遇到过这个问题),然后它才刚刚开始。 I reinstalled XAMPP, still giving me this issue.我重新安装了 XAMPP,仍然给我这个问题。

I don't get why it says using password "YES".我不明白为什么它说使用密码“是”。

I've been at this for hours trying to fix it.我一直在这几个小时试图修复它。 I've looked through lots of threads.我浏览了很多线程。 Not all necessarily apply to me, as I am running it locally, and haven't been going in and making new user accounts.不一定都适用于我,因为我在本地运行它,并且还没有进入并创建新用户帐户。 Everything is default.一切都是默认的。

Before I reinstalled, I tried creating a new user account and giving it the necessary privileges.在重新安装之前,我尝试创建一个新用户帐户并为其授予必要的权限。 I gave it a password too.我也给了密码。 Didn't work.没用。

I don't get why it is doing this, and am not knowledgeable enough to fix it myself.我不明白为什么要这样做,而且我的知识不足,无法自己解决。 Any help is much appreciated, so that it can go back to connecting.非常感谢任何帮助,以便它可以返回连接。

change改变

 $server="127.0.0.1"; 

by经过

$server="localhost";

I used XAMPP for a while and it worked quite good for me, i think the default password for your mysql server is not set to '' , so i guess if you change the root password and try accessing your database using the new password, it should work.我使用 XAMPP 有一段时间了,它对我来说效果很好,我认为你的 mysql 服务器的默认密码没有设置为'' ,所以我想如果你更改 root 密码并尝试使用新密码访问你的数据库,它应该管用。

Updating the root password更新根密码

To update the root use password, you have to go by these steps:要更新root用户密码,您必须按照以下步骤操作:

  1. Open the XAMPP Control Panel打开 XAMPP 控制面板
  2. Click on the MySQL button单击 MySQL 按钮
  3. After the MySQL console has loaded, execute this query:加载 MySQL 控制台后,执行以下查询:

    SET PASSWORD FOR 'root'@'localhost' = PASSWORD('pass');`设置密码为'root'@'localhost' = PASSWORD('pass');`

After that, change the $password variable to the new password which之后,将$password变量更改为新密码
is in this case pass在这种情况下pass

Adding a new user添加新用户

You could also add a new user by executing this query:您还可以通过执行以下查询来添加新用户:

GRANT ALL PRIVILEGES ON *.* TO 'new-user'@'localhost' IDENTIFIED BY 'password';

While the username is new-user and the password is password .用户名是new-user ,密码是password

I hope this helps.我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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