php 获取连接失败:php_network_getaddresses:getaddrinfo 失败:名称或服务未知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34266567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:51:52  来源:igfitidea点击:

Getting connection failed: php_network_getaddresses: getaddrinfo failed: Name or service not known

phpmysqlconnect

提问by Ivan Bo?njakovi?

Im using POST method to insert some data to db on my server.This is my connection.php file that is stored in my http://www.url.com/public_html.

我使用 POST 方法将一些数据插入到我服务器上的数据库中。这是我的 connection.php 文件,它存储在我的http://www.url.com/public_html 中

    <?php

$servername = "http://www.url.com";
$username = "db_username";
$password = "db_password";
$databaseName = "db_name";

$connect = new mysqli($servername,$username,$password,$databaseName);

if ($connect->connect_error) {
    die("Connection failed: " . $connect->connect_error);
} 
echo "Connected successfully";

?>

This is insert.php file also stored in http://www.url.com/public_htmlthat i use to insert data in database.

这是 insert.php 文件也存储在http://www.url.com/public_html 中,我用来在数据库中插入数据。

<?php

if($_SERVER["REQUEST_METHOD"]=="POST"){
    require'connection.php';
    createStudent();
}
function createStudent(){

    global $connect;

    $name = $_POST["name"];
    $lastname = $_POST["lastname"];
    $age = $_POST["age"];

    $query="INSERT INTO  `demo` (  `name` ,  `lastname` ,  `age` ) 
    VALUES ('$name','$lastname','$age')";
    mysqli_query($connect,$query)or die (mysqli_error($connect));
    mysqli_close($connect);
}
?>

I use postman, and my android app to test this but im getting: Connection failed: php_network_getaddresses: getaddrinfo failed: Name or service not known error.

我使用邮递员和我的 android 应用程序来测试这个,但我得到:连接失败:php_network_getaddresses:getaddrinfo 失败:名称或服务未知错误。

Im not a php developer but i can't see any error in my php files, so any help will be welcomed.

我不是 php 开发人员,但在我的 php 文件中看不到任何错误,因此欢迎提供任何帮助。

Thanks in advance.

提前致谢。

回答by Ivan Bo?njakovi?

Thanks Prerak,

谢谢普瑞克,

This is the answer, my database is on the same machine so I just needed to edit:

这就是答案,我的数据库在同一台机器上,所以我只需要编辑:

$servername = "localhost"

Now everything is working just fine.

现在一切正常。

回答by Darwin von Corax

The value you've specified for $servernameis not a host name but rather a URL, or resource name. The host name would be just www.url.com.

您指定的值$servername不是主机名,而是 URL 或资源名称。主机名将只是www.url.com.

Of course, as you've already discovered, localhostis the correct host name if the client and server reside on the same box.

当然,正如您已经发现的那样,localhost如果客户端和服务器驻留在同一个机器上,则是正确的主机名。

回答by pavan

Make changes in bellow file config-db.php

在波纹管文件中进行更改 config-db.php

nano  /etc/phpmyadmin/config-db.php 

change db server as localhost

将数据库服务器更改为本地主机

$dbserver='localhost';

Its works for me.

它对我有用。