5

I am trying to connect to my MySQL database on my online server via PHP on my localhost.

$con = mysql_connect("$hostname", "$username", "$password");
if (!$con) {die('Could not connect: ' . mysql_error());}
mysql_select_db($databasename, $con);

When I upload the script to the server it connects to the database fine. However, when I load the script on my localhost it will not connect to the online database. I shut off my firewall to make sure it was not blocking it.

Is the connection above formatted correctly to do this? Any other suggestions?

3
  • Does mysql_error() report an error when you try to connect on your localhost? Commented Jan 17, 2010 at 23:59
  • Can't connect to MySQL server on '(mysql host was here)' (10060) Commented Jan 18, 2010 at 0:01
  • Then it's definitely protected by a firewall on the server end. Commented Jan 18, 2010 at 0:33

5 Answers 5

7

Your MySQL server needs to have your IP address (or % as wild card) specified in the allowed hosts.

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

Comments

2

Does the host allow this? Many won't let external clients connect.

Comments

2

As the others say you need to allow that particular host (in this case your IP) to connect. This is in your user table in the mysql database. If you don't have access to this database (or don't have grant privileges) then you can't change this.

Comments

2

Your code is ok.

As others stated the MySQL server has to allow connections from remote machines. Also make sure your provider's firewall isn't blocking the request and double check the hostname, because it may differ from the one you use on your server.

If you didn't set up your server yourself, you'll probably have to speak with th user support of your hoster, because they have the permissions to change settings and more accurate info on this topic.

Comments

0

It is time to move to mysqli_* or PDO

Documentation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.