简体   繁体   English

PHP - 无法连接到 MySQL 数据库

[英]PHP - Cannot connect to MySQL database

I am trying to connect to my MySQL database through php, I am managing my Database with phpmyadmin.我正在尝试通过 php 连接到我的 MySQL 数据库,我正在使用 phpmyadmin 管理我的数据库。 To login the username is root and I dont have a password.登录用户名是root,我没有密码。 My problem is I cant connect, I get the "Could not connect to mySQL database" message when I try to我的问题是我无法连接,当我尝试连接时收到“无法连接到 mySQL 数据库”消息

Below is my Code下面是我的代码

 <?php

 session_start();

 $server = 'localhost';
 $db_usernmae = 'root';
 $db_password = '';
$database = 'househockey';


 if(mysql_connect($server, $db_usernmae, $db_password)){
die('Could not connect to mySQL database');
}

 if (mysql_select_db($database)) {
# code...
die('couldnt connect to database');
}

?>

Im not sure if it matters, but I am using WAMP and I put my phpmyadmin folder into my htdocs folder.我不确定这是否重要,但我正在使用 WAMP 并将我的 phpmyadmin 文件夹放入我的 htdocs 文件夹中。

Thanks谢谢

As you have written your code : 正如您编写的代码:

if(mysql_connect($server, $db_usernmae, $db_password)){
    die('Could not connect to mySQL database');
}

This will when connection is true print the following: die('Could not connect to mySQL database'); 当连接为true时,将打印以下内容: die('Could not connect to mySQL database'); I think what you need to test your connection, which sounds like it should work: 我认为你需要测试你的连接,听起来它应该工作:

if(!mysql_connect($server, $db_usernmae, $db_password)){
    die('Could not connect to mySQL database');
}

The ! ! will negate the returned value of your mysql_connect and tell you if you're connected. 将否定mysql_connect的返回值并告诉您是否已连接。

As far as I know, PMA explicitly needs a username and password. 据我所知,PMA明确需要用户名密码。 Set a root password through mysqladmin -u root password NEWPASSWORD and then change your PMA config, followed by a server restart. 通过mysqladmin -u root密码NEWPASSWORD设置root密码,然后更改PMA配置,然后重新启动服务器。 Alternatively, use MySQL workbench. 或者,使用MySQL工作台。 It does more than create entity relationship diagrams (ERDs). 它不仅仅创建实体关系图(ERD)。

You may run into this problem if you have an anonymous user defined on your database.如果您在数据库上定义了匿名用户,则可能会遇到此问题。

"When a client tries to connect to the database, the MySQL server looks through the rows in the user table in a sorted order. “当客户端尝试连接到数据库时,MySQL 服务器按排序顺序查看用户表中的行。

The server uses the first row that matches the most specific username and hostname."服务器使用与最具体的用户名和主机名匹配的第一行。”

Delete the anonymous user and try again.删除匿名用户并重试。

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

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