0

I'm getting a connection timed out when I try to connect to an external (not on the same server as this code is on) database through the following code. I know the settings of the database is set up right, and the login info is good, 'cause I can login fine from my computer with HeidiSQL. If anyone can see a problem with this code, thanks.

function database_connect(){
$link = @mysql_connect("xx.xxx.xxx.xx:3306","root","pass"); 
$sql_error = mysql_error();

if (!$link) { 
    echo "Connection with the database couldn't be made.<br>";
    echo "$sql_error"; 
    exit;
}

if (!@mysql_select_db("databasename")) {
    echo "The database couldn't be selected.";
    exit;
}
return $link;
}
database_connect();
6
  • Can you ping the MySQL server from your PHP server? Or connect with the command-line mysql program? Commented Oct 11, 2012 at 20:12
  • Who is issuing the "connection timed out"? PHP? Your browser? Commented Oct 11, 2012 at 20:12
  • @BrendanLong Nope, connection timed out too. Commented Oct 11, 2012 at 20:20
  • general tip: don't allow remote root access to the DB server. that's just begging for a world of hurt. always use limited-rights accounts. Commented Oct 11, 2012 at 20:26
  • 2
    What error are you getting exactly, can you quote it? If the connection is timing out on your server but not on your local PC, the problem might be with your server Commented Oct 11, 2012 at 20:28

2 Answers 2

1

have you tried adding the IP address of where that script is hosted to "Remote Database Access Hosts"? If you're on cPanel, you need to allow the IP address who can remotely access the datase(mysql). If you're not, then this is just an idea of what to do.

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

Comments

1

Problem known now.

My webhost does not allow connections to an external database.

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.