1

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
2
  • What's in connect.php? Commented Apr 9, 2011 at 1:50
  • Apart from your connection issue (which you should fix first), you have a problem in the assignment inside your while loop. As it is, your echo would always only output the username with the biggest id. Commented Apr 10, 2011 at 8:53

3 Answers 3

3

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.

$ mysql -u myuser -h mysql.example.com -p

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:

CREATE USER 'myuser'@'%' IDENTIFIED BY 'mypassword';

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()

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

2 Comments

Thanks, they have their port closed though, so oh well.
You can later also do GRANT ALL privileges ON newdatbase.* TO 'username'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION; and FLUSH PRIVILEGES;, for database selection if its giving you error.
1

A lot(if not all?) of hosting providers restrict remote access to the mysql database. Meaning that you can only connect via localhost. There is probably a mysql config section on the hosting provider that will allow you to config an ip address from which a certain user can connect from. Good luck.

Comments

0

Most of hosts doesn't provide a remote a access to MySQL server's as it a security risk, may you should try another free MySQL server's that allow remote access, try this but its a bit slow.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.