[SOLVED] Getting connection failed: php_network_getaddresses: getaddrinfo failed: Name or service not known

Issue

I’m 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 I’m getting:
Connection failed: php_network_getaddresses: getaddrinfo failed: Name or service not known error.

Solution

My database is on the same machine so I just needed to edit:

$servername = "localhost"

Now everything is working just fine.

Answered By – Ivan Bošnjaković

Answer Checked By – Cary Denson (BugsFixing Admin)

Leave a Reply

Your email address will not be published.