我在htdocs中有一个项目,我有一个文件connect.php 我试图连接到我们网站的数据库,因为我会在那里插入东西,但我无法以某种方式连接到数据库。
$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'];
}
不知怎的,它没有连接。这就是结果。
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
当我使用我们网站的公共IP作为主持人而不是" http://website.com/"
这部分错误消失了:
警告:PDO :: __ construct():php_network_getaddresses:getaddrinfo failed:没有这样的主机。在第15行的C:\ xampp \ htdocs \ play2winsychronizer \ connect.php
注意:我在xampp中,如果我使用localhost,我将连接到我不想要的本地数据库。我想连接到网站的数据库。
答案 0 :(得分:1)
您需要将服务器上的本地公共IP地址列入白名单。如果你有cPanel就有它的相似之处。默认情况下,大多数服务器仅配置为接受来自localhost的连接。