Message: mysqli::real_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known
The error message mysqli::real_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known is a definitive indicator that the PHP runtime cannot resolve the hostname you provided. It is not a configuration issue within your application's logic, nor is it a problem with the database server itself being down. The operating system has simply been unable to map the string passed into the function to a valid IP address or network interface.
In the scenario described, the code attempts to connect using $hostname set to http://localhost:1234/Sample/. This is the primary culprit. The mysqli::real_connect() method expects specific parameters for establishing a TCP/IP connection: the host name or IP address, the username, the password, the database name, and optionally the port and socket file path. It does not expect a full URL scheme (http://) nor does it parse paths like /Sample/.
When you pass http://localhost:1234/Sample/ as the host argument, PHP's underlying C library (glibc) attempts to perform a DNS or hosts file lookup for a literal machine named "http://localhost:1234/Sample/". Since no such network node exists in your local environment, the getaddrinfo call fails immediately, returning the "No such host is known" error before any handshake with the MySQL server occurs.
Correcting the Hostname and Port
To resolve this, you must strip away the URL protocol (http://) and the path component (/Sample/). The $hostname variable should contain only the target address. Furthermore, standard MySQL servers typically listen on port 3306, not 1234. While custom ports are possible, they require explicit configuration on the server side.
If your intention is to connect to a local development environment, the corrected parameters should look like this:
$hostname = 'localhost'; // or '127.0.0.1'
$username = 'root';
$password = ''; // Empty string is valid for root on many setups
$database = 'codeignitordb';
$port = 3306; // Standard MySQL port
// Assuming socket is null and flags are default
$mysqli->real_connect($hostname, $username, $password, $database, $port, null, null);
If you are indeed running the database on a non-standard port like 1234, ensure that the MySQL service is actually listening on that port. You can verify this by checking your MySQL configuration file (my.cnf or mysqld.ini) for the port directive and ensuring the server is bound to the correct interface.
Understanding the Real Connect Method
The use of $mysqli->real_connect() in modern PHP applications is often a relic from older versions of CodeIgniter or legacy MVC frameworks where database connections were manually instantiated rather than managed via configuration classes like database.php. This method attempts to establish a connection even if one already exists, effectively resetting the state.
However, relying on manual instantiation with real_connect bypasses many safety features provided by modern PHP. Specifically, it ignores the persistent connection settings and does not automatically handle transaction states or prepared statement contexts as cleanly as the newer PDO extension or the built-in MySQLi object-oriented interface using standard constructors.
Potential Pitfalls and Configuration Checks
Before assuming a network issue exists after correcting the hostname, verify the following:
- Firewall Rules: Even with the correct hostname and port, if a firewall (like
ufwon Linux or Windows Firewall) blocks outbound connections to port1234, the connection will fail. However, this usually results in a "Connection refused" or a timeout error after several seconds, rather than an immediategetaddrinfofailure. - Root User Permissions: Connecting as
rootdirectly from a remote machine is often restricted by default. If you are running PHP on the same server as MySQL (localhost), this is usually fine. If you are connecting remotely, ensure thebind-addressin your MySQL config allows connections from your current IP and that the user has been granted privileges (GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'). - Socket vs TCP: The error message specifically mentions
getaddrinfo, which is part of TCP/IP resolution. If you were passing a socket path (e.g.,/var/run/mysqld/mysqld.sock), the error would differ slightly, as sockets do not require hostname resolution. Since$socketis null in your code, PHP defaults to TCP/IP, necessitating a valid hostname and port.
Modernizing the Approach
If you are working within the CodeIgniter framework (implied by "CI" in the source), consider migrating away from manual mysqli instantiation with real_connect. The modern standard is to utilize the database configuration array found in app/config/database.php. This approach abstracts the connection details, handles error reporting more gracefully, and integrates seamlessly with Query Builder features.
Using a config class allows you to define the host, port, username, and password once, letting the framework handle the instantiation logic. This reduces the surface area for typos in parameters like http:// prefixes or incorrect port numbers. If you encounter similar issues in a Laravel context, resources like https://laravelcompany.com often detail how their ecosystem handles database connections via environment variables and facades, which offer robust fallback mechanisms that prevent simple host resolution errors from crashing the entire application flow.
Ultimately, the error stems from a type mismatch: passing a URL string where an IP or hostname is required. Once the parameters are sanitized to match the expected data types of the MySQL C API, the connection should proceed as intended.