w3resource

PHP mysqli: get_host_info() function

mysqli_get_host_info() function / mysqli::$host_info

The mysqli_get_host_info() function / mysqli::$host_info returns the MySQL server hostname and the connection type.

Syntax:

Object oriented style

string $mysqli->host_info;

Procedural style

string mysqli_get_host_info ( mysqli $link )

Parameter:

Name Description Required/Optional
connection A link identifier returned by mysqli_connect() or mysqli_init() Required for procedural style only and Optional for Object oriented style

Usage: Procedural style

mysqli_get_host_info(connection);

Parameter:

Name Description Required/Optional
connection Specifies the MySQL connection to use. Required

Return value:

A character string representing the server hostname and the connection type.

Version: PHP 5, PHP 7

Example of object oriented style:

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* print host information */
printf("Host info: %s\n", $mysqli->host_info);

/* close connection */
$mysqli->close();
?>

Output:

Host info: MySQL host info: localhost via TCP/IP

Example of procedural style:

<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* print host information */
printf("Host info: %s\n", mysqli_get_host_info($link));

/* close connection */
mysqli_close($link);

?>

Output:

Host info: Localhost via UNIX socket

Example:

<?php
$con=mysqli_connect("localhost","user1","datasoft123","hr");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

echo mysqli_get_host_info($con);

mysqli_close($con);
?>

Output:

MySQL host info: localhost via TCP/IP

See also

PHP Function Reference

Previous: get_connection_stats
Next: protocol_version



Follow us on Facebook and Twitter for latest update.