-1

I want to connect to a server hosted online.

My code connects to local host but when i change the values to the hosted server it gives me an error

Warning: mysqli_connect(): (HY000/2002): No connection could be made because the target machine actively refused it.

<?php

$servername = "zamokuhleWeb.co.za";
$username = "*****";
$password = "****";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";

?>

I want to the access the database and get certain information from it

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • Are you able to connect to this DB using *MySQL Workbench* ? This may be some configuration to do on your DB server – Cid Apr 03 '19 at 09:49
  • 1
    That message is usually due to MySQL not being started. But in this case it could be that you are using the wrong server name, or maybe MySQL is running on a non standard port. – RiggsFolly Apr 03 '19 at 09:49
  • 1
    Or it could be a firewall issue on your or the remote server – RiggsFolly Apr 03 '19 at 09:50
  • Check the firewall on the remote server and ensure it is allowing remote MySQL connection. You may also need to ensure your connection encryption is set (or unset) correctly. – Martin Apr 03 '19 at 09:50
  • Try [this post](https://stackoverflow.com/questions/2972600/no-connection-could-be-made-because-the-target-machine-actively-refused-it) and [this post](https://stackoverflow.com/questions/21987746/mysql-connect-no-connection-could-be-made-because-the-target-machine-actively) – Martin Apr 03 '19 at 09:51
  • You should add a port (see [this post](https://stackoverflow.com/questions/19575029/mysqli-connect-to-remote-server)) – toto1911 Apr 03 '19 at 09:52

1 Answers1

-2
<?php

$servername = "localhost";
$username = "*****";
$password = "****";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";

?>
abhayendra
  • 197
  • 6
  • OP already said that the code works if they connect to `localhost` so this is not really providing any useful information or progressing the OP to a solution – RiggsFolly Apr 03 '19 at 10:00