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
Related
I'm trying to create a login system for my website but every time I try connect it says failed.
<?php
$servername = "remotemysql.com";
$username = "no";
$password = "no";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
It's supposed to connect but it just says Connection failed: Connection timed out
I am using 000 webhost to store a sql database.
Every time I want to connect to it I recieve this error message
Warning: mysqli::__construct(): (HY000/2002): Connection refused in /storage/h10/804/1186804/public_html/Handy_Help_PHP_files/conexiune.php on line 7
Connection failed: Connection refused
This is the php I use to connect
<?php
$servername = "localhost:3306";
$username = "id1186804_admin";
$password = "12345";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Can somebody help me to find the error?
with mysqli need add database name too
<?php
$servername = "localhost:3306";
$username = "id1186804_admin";
$password = "12345";
// Create connection
$conn = new mysqli($servername, $username, $password,$yourdbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
such:
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Remove port from host parameter and put in this order:
mysqli_connect(host,username,password,dbname,port,socket);
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 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.
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.