Php – can’t connect to thesql with php


I can't seem to connect to mysql with a php script, even though I can connect fine with phpmyadmin. I created a user with a password, and gave it the proper priveleges for the db, but everytime it connects it dies saying access denied. I am using xampp on a windows xp box. Firewalls are all disabled, and I've checked the username nad password are correct. Here's the code:

$conn=mysql_connect('localhost','westbrookc16','megadots') || die (mysql_error());

Do usernames have to be in a specific format or something?

Best Solution

I have a hunch that the problem here is the host you granted it to, though it's really not more than an educated guess. If you grant access myuser@'' or the servers actual ip address, you won't be allowed to connect using localhost as host. This is due to the fact that when "localhost" is specified as host, php will assume that you want to use a unix socket instead of network sockets, and in that context isn't the same as localhost.

From the manual entry for mysql_connect():

Note: Whenever you specify "localhost" or "localhost:port" as server, the MySQL client library will override this and try to connect to a local socket (named pipe on Windows). If you want to use TCP/IP, use "" instead of "localhost". If the MySQL client library tries to connect to the wrong local socket, you should set the correct path as Runtime Configuration in your PHP configuration and leave the server field blank.

Hope this isn't totally redundant. :)