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

Cam Connor
  • 1,231
  • 2
  • 25
  • 40
  • @zerkms What does that mean? – Cam Connor May 17 '13 at 00:22
  • 1
    if you don't know what some function means - check it in manual http://php.net/mysql_error in this case – zerkms May 17 '13 at 00: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 – samayo May 17 '13 at 00: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](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) is not hard and provides you with a much better foundation for future applications. – tadman May 17 '13 at 00:34

3 Answers3

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.

usumoio
  • 3,500
  • 6
  • 31
  • 57
  • 1
    I suggest using [PHP Portable Data Objects](http://www.phpro.org/tutorials/Introduction-to-PHP-and-MySQL.html#3.3) (PDO) for DB-based code ; it's a lot easier to understand and debug. Besides, the `mysql_*` functions are deprecated. – Agi Hammerthief May 17 '13 at 00:35
  • It does not mention anything in the manual about these functions being deprecated. Are you sure that is the case? http://www.php.net/manual/en/ref.mysql.php – usumoio May 17 '13 at 14:17
  • 1
    "It does not mention anything in the manual ...", you say? [This FAQ](http://www.php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated) looks like part of the manual to me. – Agi Hammerthief May 18 '13 at 12:54
  • Interesting, you called it. I should be moving to the i extension myself, but to quote a certain manual, "this is a slow process...." :) – usumoio May 19 '13 at 04:26
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).

Agi Hammerthief
  • 2,114
  • 1
  • 22
  • 38
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.

Vincent
  • 1,741
  • 23
  • 35