- You could connect fine yesterday, and haven't changed any code in your script. Suddenly today it's not working!!
This is probably a problem with your hosting. Your hosting provider may have the databases offline for maintenance or because of an error. Contact them to see when they are expected back up.
If your database is on a different URL than the PHP file you are connecting to it from, it could be that you let your domain name expire. Sounds silly, but it happens a lot.
- I am trying to connect to localhost and it's not working!
Localhost doesn't always work, so you need to point directly to your database. Often it's something like mysql.yourname.com or mysql.hostingcompanyname.com. If you ask your hosting company they can point you in the right direction.
- I know my host name is correct, and I still can't get it to work!
Double check your username and password...then triple check them. This is one people often overlook, or check so quickly they don't even notice their mistake. Not only do you need to check that they are correct, you should make sure they have the correct permissions for what your script requires. For example, a read only user can't add data to the database... they would also need write privileges.