English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP mysqli_get_host_info() Function Usage and Example

PHP MySQLi Reference Manual

The mysqli_get_host_info() function returns a string describing the connection type used

Definition and usage

mysqli_get_host_info()The function is used to obtain information about the host, that is, the connection type used and the name of the host server.

Syntax

mysqli_get_host_info($con);

Parameter

Serial numberParameters and descriptions
1

con (optional)

This is an object representing the connection to the MySQL Server.

Return value

The PHP mysqli_get_host_info() function returns a string specifying the host name and connection type.

PHP version

This function was originally introduced in PHP version5introduced and can be used in all higher versions.

Online example

The following example demonstratesmysqli_get_host_info()Function usage (procedural style)-

<?php
  //Establish connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   //Host information
   $info = mysqli_get_host_info($con);
   print("Host information: " . $info);
   //Close connection
   mysqli_close($con);
?>

Output result

Host information: localhost via TCP/IP

Online example

In object-oriented style, the syntax of this function is$con-> host_info. The following is an example of this function in object-oriented style-

<?php
   //Establish connection
   $con = new mysqli("localhost", "root", "password", "mydb");
   //Host information
   $info = $con-> host_info;
   print("Host information: " . $info);
   //Close connection
   $con -> close();
?>

Output result

Host information: localhost via TCP/IP

Online example

The following ismysqli_get_host_infoAnother example of the function-

<?php
   //Establish connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   $code = mysqli_connect_errno();
   if ($code) {
      print("Connection failed: " . $code);
   } else {
      print("Connection established successfully" . "\n");
      $info = mysqli_get_host_info($con);
      print("Host information: " . $info);
   }
?>

Output result

Host information: localhost via TCP/IP

Online example

Return the MySQL server hostname and connection type:

<?php
   $connection_mysql = mysqli_connect("localhost","root","password","mydb");
   
   if (mysqli_connect_errno($connection_mysql)){
      echo "Cannot connect to MySQL: " . mysqli_connect_error();
   }
   echo mysqli_get_host_info($connection_mysql);   
   mysqli_close($connection_mysql);
?>

Output result

localhost via TCP/IP

PHP MySQLi Reference Manual