That is because the request is invalid, as an important credential is missing or is incorrect; or that the database is not accessible.
If the host name, username and/or password is incorrect, the SQL request will have to timeout before it can return. The standard timeout time in PHP is set to, I believe, 20 seconds, so it'll match very well to what you are experiencing. Though it will not happen with an invalid database name, as that would just make the request fail immediately.
To check for an error, you can call mysql_error()
to get the specification of the error from PHP, or mysql_errno()
to get just the error code. The standard timeout time can be set to a lower value by editing the php.ini file (if you have access to it); set the value followed by mysql.connect_timeout
(that is, in php.ini) to whatever timeout time you'd want, in seconds. In normal situations, a timeout time of 5 seconds or more is good, perhaps even unnecessarily big.