I have a MySQL database set up on 000webhost.com. I want to access the data in my database via PHP. I tried:
<?php
include("connect.php");
mysql_select_db("XXXXXXX_users", $con) or die(mysql_error());
$result = mysql_query("SELECT * FROM data ORDER BY id DESC");
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
$user = $row['usrname'];
}
echo "$user";
?>
But it always returns as: “A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.”
What do I do?
- I do not have root access
- I’m trying to run the script on Apache
When debugging this sort of thing, it’s always good to make sure you can connect using the command line mysql tool first, to rule out issues related to PHP itself.
You’ll get a more descriptive error message as well, e.g.
ERROR 1045 (28000): Access denied for user 'root'@'remote.example.com' (using password: YES)If the mysql port is even open on a public interface to begin with (which is usually a terrible idea), mysql itself has its own layer of access control based on the connecting host. You may need to have an account created specifically with a wildcard hostname, like so:
The
@'%'part, specifically, allows connection from any host, local or remote.Once you can successfully connect from the command line, it’s then a simple issue of replicating your command line arguments as arguments to
mysql_connect()