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

↘锁芯ラ 提交于 2019-11-28 07:46:43

问题


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.

    <?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_html that i use to insert data in database.

<?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.

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

Thanks in advance.


回答1:


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.




回答2:


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

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




回答3:


Make changes in bellow file config-db.php

nano  /etc/phpmyadmin/config-db.php 

change db server as localhost

$dbserver='localhost';

Its works for me.



来源:https://stackoverflow.com/questions/34266567/getting-connection-failed-php-network-getaddresses-getaddrinfo-failed-name-or

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!