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();
share|improve this question
Can you ping the MySQL server from your PHP server? Or connect with the command-line mysql program? – Brendan Long Oct 11 '12 at 20:12
Who is issuing the "connection timed out"? PHP? Your browser? – Pekka 웃 Oct 11 '12 at 20:12
@BrendanLong Nope, connection timed out too. – gdscei Oct 11 '12 at 20:20
@Pekka PHP gives the error. – gdscei Oct 11 '12 at 20:21
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 – Pekka 웃 Oct 11 '12 at 20:28
show 2 more comments

3 Answers

Did you check if the user root may have access to login from external host? because when i installed mysql by default it was configured to have access from localhost

share|improve this answer
As I said in the question, I can login fine from HeidiSQL from my computer. – gdscei Oct 11 '12 at 20:21
if it was a permissions problem, you'd get a "permission denied" error, not a timeout. – Marc B Oct 11 '12 at 20:26

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.

share|improve this answer

Problem known now.

My webhost does not allow connections to an external database.

share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.