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.
Related
I added a database to my cPanel, but I cannot connect it.
try{
$db = new
PDO('mysql:host=markus.veridyen.com;dbname=sosyalki_pratiki1_ipss;charset=utf8','sosyalki','password');
}catch(PDOException $e){
echo 'Hata: '.$e->getMessage();
}
When I try to connect my database, it gives me this error message:
Hata: SQLSTATE[HY000] [2002] The connection could not be established because the target machine actively refused.
Notice: Undefined variable: db in C:\xampp\htdocs\ipss\survey.php on line 55
You still can use localhost as server name on a live server.
I'll assume you are using cpanel.
<?php
$servername = "localhost";
$username = "username"; //your cpanel username
$password = "password"; //your cpanel password
try {
$conn = new PDO("mysql:host=$servername;dbname=DatabaseName", $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();
}
?>
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
I'm new to web development. I bought a server on DigitalOcean, created a droplet and installed LAMP. I have a simple PHP script to connect to MySQL. Here's my PHP script(conn.php)
<?php
$servername = "xxx.xx.xx.xxx";
$username = "root";
$password = "my_password";
try {
$conn = new PDO("mysql:host=$servername;dbname=mysql", $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();
}
?>
Now, when I try to run on my local "xxx.xx.xx.xxx/conn.php", it's printing
Connection failed: SQLSTATE[HY000] [2002] Connection refused
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";
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?