• PHP Video Tutorials

PHP mysqli_get_host_info() Function



Definition and Usage

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

Syntax

mysqli_get_host_info($con);

Parameters

Sr.No Parameter & Description
1

con(Optional)

This is an object representing a connection to MySQL Server.

Return Values

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

PHP Version

This function was first introduced in PHP Version 5 and works works in all the later versions.

Example

Following example demonstrates the usage of the mysqli_get_host_info() function (in procedural style) −

<?php
  //Creating a connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   //Host Info
   $info = mysqli_get_host_info($con);
   print("Host Info: ".$info);

   //Closing the connection
   mysqli_close($con);
?>

This will produce following result −

Client Library Version: localhost via TCP/IP

Example

In object oriented style the syntax of this function is $con -> host_info. Following is the example of this function in object oriented style −

<?php
   //Creating a connection
   $con = new mysqli("localhost", "root", "password", "mydb");

   //Host Info
   $info = $con->host_info;
   print("Host Info: ".$info);

   //Closing the connection
   $con -> close();
?>

This will produce following result −

Client Library Version: localhost via TCP/IP

Example

Following is another example of the mysqli_get_host_info function −

<?php
   //Creating a 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 Info: ".$info);
   }
?>

This will produce following result −

MySQL Server Version Number: localhost via TCP/IP

Example

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

This will produce following result −

localhost via TCP/IP
php_function_reference.htm
Advertisements