Warning: mysql_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known
When you see the error php_network_getaddresses: getaddrinfo failed: No such host is known, it usually indicates a DNS resolution failure rather than a simple database authentication issue. The specific message tells you that PHP cannot translate the hostname provided in your connection string into an IP address. In your case, the server name is set to localhost. While this seems trivial for a local development environment running XAMPP, the underlying mechanism treats localhost as a DNS query just like any other domain name.
The error log points to line 7 in your file wf-insertcase.php, where the connection logic resides. You are using the deprecated mysql_connect() function from the legacy MySQL extension. Before diving into the specific resolution, it is critical to address the architecture of your code. The mysql_* functions were removed in PHP 5.5 and completely removed in PHP 7.0 because they do not support prepared statements, making them inherently vulnerable to SQL injection attacks. If you are writing new code or refactoring an existing application, switching to PDO or MySQLi is mandatory. However, even if you stick with the legacy API for a quick fix in a controlled environment, understanding why this specific error occurs is vital.
The root cause of No such host is known with localhost lies in how your operating system resolves names. When PHP attempts to connect to localhost, it expects the system's resolver (often configured via /etc/hosts on Linux or the Windows Hosts file) to map that string to 127.0.0.1. If this mapping is missing, corrupted, or if your PHP configuration forces DNS lookups for names starting with "local", the request fails immediately before any network socket is even opened.
To resolve this on a Windows environment like XAMPP, you must ensure that the Hosts file contains the standard entry for localhost. Open the C:\Windows\System32\drivers\etc\hosts file in a text editor with administrator privileges and verify it contains the following line:
127.0.0.1 localhost
If this line is missing, add it. However, there is a more robust solution that addresses both the legacy syntax issue and the connection reliability. The mysql_connect() function accepts four arguments: username, password, host, and port/database name. Your code passes the database name as the fourth argument, but the standard signature for mysql_connect does not accept a database name in that position. It only accepts the host string. To select a specific database after connecting, you must use mysql_select_db(). Passing the database name directly into mysql_connect() is incorrect and may cause unpredictable behavior depending on the library version.
Here is how you should refactor the connection logic to be both functional and secure:
<?php
define('dbuser', 'pma');
define('dbpass', '');
define('dbserver', 'localhost');
define('dbname', 'wikifiesto');
// Connect first, passing only username, password, and host.
$conn = mysql_connect(dbuser, dbpass, dbserver);
if (!$conn) {
// Log the specific error to help with debugging
die('Error connecting to database: ' . mysql_error());
}
// Select the database after connection is established
mysql_select_db(dbname);
echo 'You have created case';
?>
While fixing the Hosts file might clear the getaddrinfo warning, relying on the mysql_* extension is not a sustainable strategy. Modern PHP development utilizes PDO or MySQLi, which offer superior error reporting and security features. If you are building a production-ready stack or migrating away from XAMPP's default configuration, consider adopting a framework like Laravel. The LaravelCompany team provides extensive documentation on configuring database connections using the .env file and environment variables, which abstracts away these low-level connection string issues entirely.
For instance, in a modern application, you would define your credentials in the environment variables:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_DATABASE=wikifiesto
DB_USERNAME=pma
DB_PASSWORD=
This approach eliminates the need to hardcode connection strings in PHP files and ensures that localhost is resolved correctly by using an explicit IP address if DNS resolution remains problematic. By correcting the Hosts file entry and updating your code to use the correct function signatureâor better yet, migrating to a modern driverâyou eliminate both the immediate error and the long-term security risks associated with legacy MySQL extensions.