'Can't connect to MySQL via PHP "Name or service not known"

I have a website, which sends input data with AJAX via a PHP script to a MySQL database.

The JavaScript/AJAX part is working but the PHP script doesn't get executed, and if I paste the URL from the PHP script into my browser I get:

Connection failed:php_network_getaddresses: getaddrinfo failed: Name or service not known

The host, server, password and database are 100% correct.

My PHP code is

<?php

$host = '';
$user = '';
$pswd = '';
$database = '';

$conn = new mysqli($host, $user, $pswd, $database);

if ($conn->connect_error) {
  die ("Connection failed:" . $conn->connect_error);
}
echo "Connection successful";

$name = $_POST['name'];

$sql = "INSERT INTO Names(s) VALUES(?)";


if ($stmt = mysqli_prepare($conn, $sql)) {

$sql->bind_param('s', $name);

  $stmt = mysqli_parse($conn, $sql);
  $stmt->execute();

  echo "Created successfully";
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

?>


Solution 1:[1]

Besides the initial issue. You have it a bit jumbled up. The bind_param is off.

if ($stmt = mysqli_prepare($conn, $sql)) {
    $stmt->bind_param('s', $name);

But, since you don't make it past die(), there is something wrong with either of $host, $user, $pswd, $database or maybe a firewall or DNS issue somewhere. Or the server might not be running as mentioned in the comments.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Guido Faecke