get_host_info
In this article, we will focus on the mysqli_get_host_info() function in PHP, which is used to return the host information for a MySQL connection. We will
The mysqli_get_host_info() function returns a single string that describes how your PHP script is connected to the MySQL server — the host name together with the transport (TCP/IP, a Unix socket, or a named pipe). This page explains the syntax, the values you can expect back, both the procedural and object-oriented styles, and the practical cases where the function is worth reaching for.
What get_host_info() returns
mysqli_get_host_info() is a built-in PHP function that, given an open MySQLi connection, returns a human-readable string describing the connection type and host. Typical return values are:
localhost via TCP/IP— connected to the local server over a network socket.Localhost via UNIX socket— connected through a local Unix domain socket (the default on Linux/macOS when the host islocalhost).127.0.0.1 via TCP/IP— connected to an explicit IP over TCP/IP.db.example.com via TCP/IP— connected to a remote host.
It is read-only and never changes the connection; it simply reports the transport the driver negotiated. That makes it handy for debugging (confirming you really hit the socket/host you expected) and for logging connection diagnostics.
Syntax
mysqli_get_host_info(mysqli $mysqli): string| Parameter | Description |
|---|---|
$mysqli | A valid connection object returned by mysqli_connect() or mysqli_real_connect(). |
Return value: a string describing the server connection type. It never returns false; if the link is invalid PHP raises a warning instead.
Procedural style
Pass the connection object returned by mysqli_connect():
<?php
$mysqli = mysqli_connect("localhost", "username", "password", "database");
if (!$mysqli) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Host information: " . mysqli_get_host_info($mysqli);
mysqli_close($mysqli);
?>We first call mysqli_connect() to open the connection, then check the result so we can fail cleanly with mysqli_connect_error() instead of triggering warnings. Passing the link to mysqli_get_host_info() returns the transport description, which we print. On a Linux host using localhost, the output looks like:
Host information: Localhost via UNIX socketObject-oriented style
The procedural function has an equivalent property on the mysqli object, get_host_info:
<?php
$mysqli = new mysqli("localhost", "username", "password", "database");
if ($mysqli->connect_errno) {
die("Connection failed: " . $mysqli->connect_error);
}
echo "Host information: " . $mysqli->get_host_info;
$mysqli->close();
?>Both styles return the same string. Note that in the object-oriented form get_host_info is accessed as a property, not called as a method.
When would I use it?
- Verifying the transport. Forcing TCP/IP (e.g. by using
127.0.0.1instead oflocalhost) is sometimes needed for tooling or TLS.get_host_info()confirms which one you actually got. - Connection logging. Recording the host string alongside connection stats gives you a quick at-a-glance record of where a request connected.
- Troubleshooting "it works locally but not in production." A surprising
via UNIX socketvsvia TCP/IPoften explains permission or firewall differences.
Common gotchas
- Do not confuse it with the
$_SERVERsuperglobal.$_SERVER['SERVER_NAME']describes the web server handling the HTTP request;mysqli_get_host_info()describes the database connection — two unrelated things. - The string is for humans, not parsing. If you need structured values, inspect the host you passed to
mysqli_connect()rather than splitting this string. - For the MySQL client library version (rather than the connection host), use
mysqli_get_client_info(); for the server side, usemysqli_get_server_info().
Conclusion
mysqli_get_host_info() is a small but practical diagnostic function: it reports how your script reached the MySQL server. Use it when you need to confirm or log the connection transport, and reach for mysqli_get_proto_info() or mysqli_get_connection_stats() when you need more detail.