PHP mysqli_get_host_info() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_get_host_info() returns a string describing the host and connection type used by an open mysqli link.

Typical values look like "localhost via UNIX socket" or "db.example.com via TCP/IP". The object style is $mysqli->host_info. Call it after a successful connect.

Syntax

Call mysqli_get_host_info() with the values you need. Start with this line.

PHP
$info = mysqli_get_host_info($mysqli);
CallWhat it does
mysqli_get_host_info($mysqli)Returns the host info string for that connection.
$mysqli->host_infoOO property with the same information.
echo mysqli_get_host_info($mysqli);Prints how PHP is connected to MySQL.

Useful in diagnostics when you are unsure whether PHP used a socket or TCP. It does not open a new connection; it only describes the existing one.

Library declarations
PHP
mysqli_get_host_info(mysqli $mysql): string

Parameters

ParameterDescription
mysqlRequired. An open mysqli connection.

Return Value

Return ValueDescription
stringA description of the host and connection type.

PHP Examples

Example 1 — Print Host Info After Connect

Connect to a local demo server and print a typical host info string.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
echo mysqli_get_host_info($mysqli), PHP_EOL;
mysqli_close($mysqli);

How It Works

  1. mysqli_connect() opens the demo link.
  2. mysqli_get_host_info() describes that link.
  3. The sample shows a UNIX socket connection to localhost.
  4. TCP setups print a via TCP/IP style message instead.

Example 2 — TCP Host Info Sample

Show what a remote TCP connection string often looks like in demos.

PHP
<?php
$mysqli = mysqli_connect("db.example.com", "demo", "secret", "shop");
echo mysqli_get_host_info($mysqli), PHP_EOL;
mysqli_close($mysqli);

How It Works

  1. The host name is a remote demo server.
  2. PHP connects over TCP/IP in this sample.
  3. get_host_info() includes the host and transport.
  4. Exact text can vary slightly by driver.

Example 3 — Object Property Form

Read host_info from the mysqli object after connecting.

PHP
<?php
$mysqli = new mysqli("localhost", "demo", "secret", "shop");
echo $mysqli->host_info, PHP_EOL;
$mysqli->close();

How It Works

  1. new mysqli() establishes the connection.
  2. The host_info property is read directly.
  3. Sample output matches the procedural call.
  4. close() ends the session.

Frequently Asked Questions

It returns a string describing which host the connection uses and whether it is a socket or TCP/IP link.
Read $mysqli->host_info on the connection object.
After a successful connection. It describes the current link.
No. It only reports information about the existing connection.
On many Unix-like systems, connecting to localhost uses a filesystem socket instead of TCP. That is normal.

Next: mysqli_get_links_stats()

mysqli_get_links_stats() returns an associative array with counts of mysqlnd-tracked mysql.

mysqli_get_links_stats() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful