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

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

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Diagnosing php_network_getaddresses: getaddrinfo failed in PHP MySQLi Connections

When a PHP application throws Connection failed: php_network_getaddresses: getaddrinfo failed: Name or service not known, it is almost never an issue with the database engine itself. The error originates from the operating system's networking stack attempting to resolve a hostname before establishing a TCP connection. If that resolution fails, the mysqli extension cannot proceed.

In the scenario provided, the developer attempts to connect to a database using the following configuration:

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

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

The critical failure point here is the value assigned to $servername. The string "http://www.url.com" contains a protocol scheme (http://). PHP's mysqli extension expects a standard hostname or IP address, such as www.url.com or 192.168.1.50. When you pass a URL with a scheme to the connection function, the underlying C library tries to treat the entire string, including the http:// prefix, as a host header for DNS resolution.

The operating system's resolver then attempts to look up a domain literally named "http://www.url.com". Since no such domain exists in any public or private DNS zone, the lookup fails immediately. The error message getaddrinfo failed: Name or service not known is the standard POSIX response to this specific failure mode.

Correcting the Hostname

To resolve this, you must strip the protocol scheme and pass only the host identifier. Update the connection string as follows:

// Remove 'http://' or 'https://' from the server name
$servername = "www.url.com"; 

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

If you are accessing a database running locally on the same server, ensure you are not using localhost if your MySQL configuration restricts binding to 127.0.0.1. In many production environments, it is best practice to use a short host alias defined in /etc/hosts or rely on the database server's hostname directly.

Network Configuration and Firewall Rules

Even with the correct hostname, connection failures can stem from firewall restrictions. The error getaddrinfo failed specifically indicates a DNS resolution issue, but developers often confuse this with "Connection refused" errors caused by firewalls.

If you have corrected the hostname and the error persists, verify that the database server is listening on the correct interface. By default, MySQL often binds only to 127.0.0.1. If your PHP script runs on a different machine or via load balancer, you must configure the MySQL bind-address directive in my.cnf to allow remote connections, or ensure the firewall allows traffic on port 3306 from the application server's IP range.

Security Considerations: Code Review and Best Practices

The provided code snippets also present significant security vulnerabilities that should be addressed alongside connection fixes. The insert.php file retrieves user input directly via $_POST and concatenates it into a SQL query string:

$query = "INSERT INTO `demo` (`name`, `lastname`, `age`) VALUES ('$name','$lastname','$age')";
mysqli_query($connect, $query);

This approach is vulnerable to SQL injection. An attacker could manipulate the POST data to alter the query structure and access or corrupt data. You must use prepared statements with bound parameters to sanitize input automatically.

Furthermore, loading configuration details like database credentials directly into a PHP file (connection.php) that is included by other scripts increases the risk of exposure if insert.php becomes accessible via direct URL traversal. It is standard practice to store credentials in environment variables or a secure configuration file outside the web root.

For developers working with modern PHP stacks, integrating these practices into a framework like Laravel can mitigate many of these risks out of the box. Resources such as https://laravelcompany.com offer guidance on securing database connections and managing configuration securely within enterprise-grade applications.

Our network