I was connected with my server using my server name.
<?php
ini_set('default_charset', 'UTF-8');
$servername = "my server name here";
$username = "xxxxx";
$pass = "xxxx";
$dbname = "xxxx";
$conn = mysqli_connect ($servername, $username, $pass, $dbname);
if(!$conn) {
die("Connection faild: ".mysqli_connect_error());
}
$conn->query("SET NAMES utf8");
?>
All my aplications was working and now stopped.
Now is only connecting if i use my server ip.
There are the errors
**Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed
Connection faild: php_network_getaddresses: getaddrinfo failed
**
When i connect on the database with my login and pssword in my host provider
it enters fine,nothing changed.
Btw im using xampp.
Anyone knows why this is happening?
Use that
$db_conx = mysqli_connect("localhost", "root", "", "project2");
// Evaluate the connection
if (mysqli_connect_error()) {
echo mysqli_connect_error();
exit();
}
Normally, using xampp in Windows, we will set server name be "localhost"
$servername = "localhost";
If you want other name, please set in hosts file:
C:\Windows\System32\drivers\etc\hosts
127.0.0.1 my_domain_name.com
And add "my_domain_name.com" (for example) into $servername variable
Related
I have bought a database MySQL to use on my domain/host.
I can connect to the database via wampserver (localhost) without problems.
But when I am trying to connect to the host website it gives me an error.
Connection failed: Can't connect to MySQL server on 'mysql-******.net' (111 "Connection refused")
My code:
<?php
$servername = "mysql-*********.net";
$username = "username8954";
$password = "dolphyn1235";
$dbname = "animals";
$dbServerPort = "26313";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname, $dbServerPort);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Any ideas how to solve this?
Check in your MySQL Server configuration files (my.cnf), find bind-address = 127.0.0.1 then comment them with # at the begining of the line. and then restar your MySQL.
my code is as below
$dbhost = 'example.com';
$dbuser = 'dbusername';
$dbpass = 'dbpassword';
$dbName='dbname';
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbName);
// check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
after i execute code getting below error
Connection failed: Connection refused
Warning: mysqli::__construct(): (HY000/2002): Connection timed out in /home/public_html/test/remote.php on line 8
Connection failed: Connection timed out
I don't know your environment, but my experience is that most publicly accessible webservers do not expose the SQL-database to the public, so the port is probably blocked. If you control the server, then you can probably find something in the firewall, but if you are using something like shared hosting, then you can really only try the helpdesk.
This question already has answers here:
PHP Connection failed: SQLSTATE[HY000] [2002] Connection refused
(10 answers)
Closed 3 years ago.
I am new in php and doing a login/register page in localhost using xampp on mac. I am having a trouble with this error:
Warning: mysqli_connect(): (HY000/2002): Connection refused in /opt/lampp/htdocs/website/includes/dbh.inc.php on line 9
Connection failed: Connection refused
I have tried changing the code but it continuously adding more problems.
This is my code:
<?php
$servername = "192.168.64.2:8080";
$dBUsername = "root";
$dBPassword = "";
$dBName = "loginsystem";
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName);
if (!$conn) {
die("Connection failed: ".mysqli_connect_error());
}
How can I eliminate this error?
If you want to connect to other port than the default one, add the port argument when you establish the connection, not define it within the server name.
$servername = "192.168.64.2";
$dBUsername = "root";
$dBPassword = "";
$dBName = "loginsystem";
$dbPort = "8080";
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName, $dbPort);
if (!$conn) {
die("Connection failed: ".mysqli_connect_error());
}
Another thing is to make sure you have the right port of MySQL service, port 8080 is commonly picked as the alternative for port 80 to handle HTTP request. Check your XAMPP configuration to find out which port is used for MySQL.
I am using Xampp to update my website and since I have added a Mysql database I have the error further down locally while it works fine online.
I'm using the code below to connect to my database:
<?php
$servername = "myservername";
$username = "myusername";
$password = "mypassword";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
http://localhost/connectdb.php
Result:
Connection failed: php_network_getaddresses: getaddrinfo failed: No
such host is known.
http://example.com/connectdb.php
Result:
Connected successfully
So there must be some configuration to do in Xampp to access a remote database or do I need to ask my host to whitelist my private IP Address? My host is OVH.
Thanks for your help
I have created a database using webmatrix which I'm unable to connect. Here is my web.config file.
<add connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=EmptySite10;User ID=sa;Password=serial" name="EmptySite10" providerName="System.Data.SqlClient" />
Here is my PHP connect code.
$servername = ".\SQLEXPRESS";
$username = "sa";
$password = "serial";
$dbname = "EmptySite10";
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Could not connect : " . mysqli_connect_error());
mysqli_close($conn);
I get the following error:
Could not connect : php_network_getaddresses: getaddrinfo failed: No such host is known.
How can I connect to database?
$servername = "127.0.0.1";
enter the ip address or hostname in $servername.