简体   繁体   中英

PHP - Cannot connect to MySQL database

I am trying to connect to my MySQL database through php, I am managing my Database with phpmyadmin. To login the username is root and I dont have a password. My problem is I cant connect, I get the "Could not connect to mySQL database" message when I try to

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.

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'); 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.

As far as I know, PMA explicitly needs a username and password. Set a root password through mysqladmin -u root password NEWPASSWORD and then change your PMA config, followed by a server restart. Alternatively, use MySQL workbench. It does more than create entity relationship diagrams (ERDs).

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.

The server uses the first row that matches the most specific username and hostname."

Delete the anonymous user and try again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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