3

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

4
  • @zerkms What does that mean? Commented May 17, 2013 at 0:22
  • 1
    if you don't know what some function means - check it in manual php.net/mysql_error in this case Commented May 17, 2013 at 0:22
  • @user2109242 it means you should add mysql_query() to your queries so, mysql will tell you in plain/easy English why it is not working. Try doing mysql_connect() or die(mysql_error()); and mysql_connect() or die(mysql_error()); the ` or die(mysql_error());` is error, handler and will hel you debug your errors Commented May 17, 2013 at 0:26
  • If this is the beginning of an application, please DO NOT USE mysql_query. It's deprecated, dangerous if used incorrectly, and will be removed in future versions of PHP. Learning PDO is not hard and provides you with a much better foundation for future applications. Commented May 17, 2013 at 0:34

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I suggest using PHP Portable Data Objects (PDO) for DB-based code ; it's a lot easier to understand and debug. Besides, the mysql_* functions are deprecated.
It does not mention anything in the manual about these functions being deprecated. Are you sure that is the case? php.net/manual/en/ref.mysql.php
"It does not mention anything in the manual ...", you say? This FAQ looks like part of the manual to me.
Interesting, you called it. I should be moving to the i extension myself, but to quote a certain manual, "this is a slow process...." :)
0

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

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.