0

What I'm trying to do is connecting to server database from localhost.

$host = 'http://www.my-domain.com/phpmyadmin/';
$user = 'u5er';
$pass = 'pa55w0rd';
$db = 'db_name';

$con = mysqli_connect($host,$user,$pass,$db) or die("Error " . mysqli_error($con));
$sql = "SELECT col FROM test WHERE id = '1'";

$result = mysqli_query($con,$sql);

Errors

Warning: mysqli_connect(): in C:\xampp\htdocs\sp_concord\cenova_nabidka\page\vytvor.php on line 362

Warning: mysqli_connect(): in C:\xampp\htdocs\sp_concord\cenova_nabidka\page\vytvor.php on line 362

Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\sp_concord\cenova_nabidka\page\vytvor.php on line 362
Error

Is it possible to resolve it? How to find out if my server allows external connections? And how to define from which IP addresses it should allow access to database?

Thank you for any suggestion.

EDIT: Ok, I have changed the host to my-domain.com and now it reports the following errors. My IP has no acces to MySQL server...

Warning: mysqli_connect(): (HY000/1130): Host '88.146.210.54' is not allowed to connect to this MySQL server in C:\xampp\htdocs\sp_concord\cenova_nabidka\page\vytvor.php on line 362
Failed to connect to MySQL: Host '88.146.210.54' is not allowed to connect to this MySQL server
Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\sp_concord\cenova_nabidka\page\vytvor.php on line 368
falcon
  • 357
  • 2
  • 5
  • 21
  • 1
    very unlikely that host string is correct. phpmyadmin is NOT a data base –  May 25 '14 at 23:32

6 Answers6

2

if you upload your localhost file to web server, some host provider, use "localhost" too as hostname

$host = 'localhost';
$user = 'u5er';
$pass = 'pa55w0rd';
$db = 'db_name';

"Host 'XXX.XXX.XXX' is not allowed to connect to this MySQL server" appear when you didn't configure host to grant accessing database

Reids Meke Meke
  • 367
  • 4
  • 14
  • Hi, I tried to configure an access to dababase for my IP address, but with no success yet. As soon as I have time for this, I will fix it. Thank you. – falcon May 26 '14 at 05:58
1

Your logging has an error is on the following line:

$con = mysqli_connect($host,$user,$pass,$db) or die("Error " . mysqli_error($con));

$con is never assigned a value (base mysqli_connect failed) and you are passing it to mysqli_error().

Try the following instead - it will give you the information you need about why you cannot connect:

$con = mysqli_connect($host,$user,$pass,$db);
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

As user Dagon mentioned, your host string should be something like my-domain.com or even more likely, localhost and not http://www.my-domain.com/phpmyadmin/

Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
  • Is the MySQL database running on the same server the PHP script? In that case you can just change the host to `localhost` instead of `my-domain.com`. If it's not you will need to configure MySQL to allow connections from remote machines - take a look at the link here: http://stackoverflow.com/questions/8348506/grant-remote-access-of-mysql-database-from-any-ip-address – Martin Konecny May 25 '14 at 23:42
  • No, it's not and I'm going to check your suggestions. Thank you. – falcon May 25 '14 at 23:45
  • 1
    Prosim :). BTW, here's a better link: http://www.rackspace.com/knowledge_center/article/mysql-connect-to-your-database-remotely – Martin Konecny May 25 '14 at 23:45
1

Most external databases are connected to via an IP or domain/subdomain with correct port number. You have put "http" which is port 80. Most MySQL are 3306. Your host/extremal provider should have given you an IP of the database server and logon details...

Kinnectus
  • 899
  • 6
  • 14
1

Your IP address must be registered to your host in order to connect to the database

1

Yes, it's possible to resolve that. Change the variable $host to the right hostname (localhost or other).

To find out if your server allows external connections you can use an online port scan to see if you have the mysql port (default is 3306) open to the outside world.

To define which IP addresses can access your database you should use an firewall rule.

bitfhacker
  • 292
  • 1
  • 13
0

The host string is wrong. A valid mysql URL will be of the form 'mysql://hostname.tld/database'. However its very unusual that administrators make SQL databases available over the internet. Most likely you should try to run your php application from the same server or network as the database server.

DMCoding
  • 1,167
  • 2
  • 15
  • 30
  • Hi, thank you, but it's not possible. This is local application and I need to use some data from server my-domain.com – falcon May 25 '14 at 23:42