PHP Mysqli fail to getaddress - php

I get the following error when I try to establish a connection to my database using mysqli:
Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo
failed: No such host is known. in
C:\xampp\htdocs\emarps\database\getuserdetails.php on line 4
Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is known.
in C:\xampp\htdocs\emarps\database\getuserdetails.php on line 4
Connect failed: php_network_getaddresses: getaddrinfo failed: No such
host is known.
Below is my php code for connecting to the database:
$host_name = "127.0.0.1";
$database = "db565263480";
$user_name = "dbo565263480";
$password = "emarps2015!";
$link = mysqli_connect('localhost', $user_name, $password, $database);
// check connection
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
Please advise what am I doing wrong?

Related

Is there a way to find servername, username and password of an MySQL server if I can log in via phpmyadmin?

Imagine I can access a MariaDB Database via phpmyadmin, e.g.
https://test.com/phpMyAdmin
username: some_username
password: some_password
Now I just want to use php to search whether some value exists; However, to do so I need to connect to the database with a servername, username and password;
I tried the servername
https://test.com`
with username and password as above but no luck; Is there a way to find that out?
Right now my currect code looks like that:
<?php
$servername = "https://test.com";
$username = "some_username";
$password = "some_password";
$conn = new mysqli($servername, $username, $password, "DB1");
// Überprüfe Verbindung
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
which gives me the error
Warning: mysqli::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\test.php on line 9
Warning: mysqli::__construct(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\test.php on line 9
Connection failed: php_network_getaddresses: getaddrinfo failed: No such host is known.

mysql: Cpanel Database connection php_network_getaddresses error

I am facing database connection error in Cpanel hosting with below code;
<?php
$servername = "localhost";
$username = "helloDB";
$password = "MY_PASS";
$conn = mysqli_connect($servername, $username, $password);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
Error Log...
Connection failed: SQLSTATE[HY000] [2002] php_network_getaddresses:
getaddrinfo failed: Name or service not known

Connection failed mysqli php

I have set a connection to a mysql server (OVH) in PHP like this :
$con = mysqli_connect("myserver.mysql.db","user","pass","bdd") or die("Couldn't connect");
And it can't connect it, saying the host in unknow. But this is the only informations I have about the server
What can I do to get over this problem ?
Thanks
EDIT :
Here is php code :
class dbconnection{
function connect(){
$con = mysqli_connect("myserv.mysql.db","user","mdp","bdd") or die("Couldn't connect" . $con->connect_error);
return $con;
}
}
Then :
$con = new dbconnection();
$db = $con -> connect();
And here is the error message :
Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: unknown host. in C:\xampp\htdocs\hackaton\class\dbconnection.php on line 11
Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: unknown host. in C:\xampp\htdocs\hackaton\class\dbconnection.php on line 11
Notice: Trying to get property of non-object in C:\xampp\htdocs\hackaton\class\dbconnection.php on line 11
Couldn't connect

getaddrinfo failed: No such host is known [duplicate]

This question already has answers here:
Message: mysqli::real_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known
(2 answers)
Closed 1 year ago.
I tried to connect to phpMyAdmin database but encounter an error Warning: mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: No such host is known. Below coding is the one that I currently use.
$servername = "http://localhost:8080";
$username = "root";
$password = "";
$db = "dbLogin";
$conn = new mysqli($servername, $username, $password, $db);
if ($conn->connect_error) {
die("Connection failed: ".$conn->connect_error);
}
else {
echo "Connection success!";
}
When I run my system, It will still show the message connection success! but together with the error I mentioned.
replace server name from
$servername = "http://localhost:8080";
to
$servername = "localhost";

Hosting MySQL connection

I am using this script to connect to a database. But it is throwing me this exception. Need Help!
<?php
$servername = "xxxx";
$username = "xxxx";
$password = "xxxx";
try {
$conn = new PDO("mysql:host=$servername;dbname=my_db", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
The exception :
Warning: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\Sites\mysql\index.php on line 8
Connection failed: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known.

Categories