I have a project in htdocs and i have a file connect.php I'm trying to connect to our website's database because i will be inserting stuff in there but i can't connect to the db somehow.
$servername = "http://websitename.com";
$username = "root";
$password = "websitepassword";
try {
$conn = new PDO("mysql:host=$servername;dbname=coverview", $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();
}
$resulttwo = $conn->prepare("SELECT * FROM tablename;");
$resulttwo->execute();
while ($rowtwo = $resulttwo->fetch(PDO::FETCH_ASSOC))
{
echo $rowtwo['fieldname'];
}
somehow it is not connecting. this is the result.
Warning: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\play2winsychronizer\connect.php on line 15
Connection failed: SQLSTATE[HY000] [2002] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
Notice: Undefined variable: conn in C:\xampp\htdocs\play2winsychronizer\connect.php on line 27
Fatal error: Call to a member function prepare() on null in C:\xampp\htdocs\play2winsychronizer\connect.php on line 27
when i use our website's public IP for the host instead of "http://website.com/"
this part of the error disappears:
Warning: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\play2winsychronizer\connect.php on line 15
NOTE: I am in xampp and if i use localhost, i will be connected to my local database which i don't want. i want to connect to a website's database.
You need to whitelist you local public ip address on your server. If you have cPanel there is a like for it. By default most servers are only configured to accept connections from localhost.