• PHP Video Tutorials

PHP mysqli_get_server_version() Function



Definition and Usage

The mysqli_get_server_version() function returns the version number of the MySQL Server currently you have connected to.

Syntax

mysqli_get_server_version($con);

Parameters

Sr.No Parameter & Description
1

con(Mandatory)

This is an object representing a connection to MySQL Server.

Return Values

PHP mysqli_get_server_version() function returns a integer value representing the version of underlying MySQL Server to which a connection has been established.

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_server_version() function (in procedural style) −

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

   //MySQL server version
   $version = mysqli_get_server_version($con);
   print("Client Library Version Number: ".$version);

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

This will produce following result −

Client Library Version Number: 50712

Example

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

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

   //MySQL server version
   $version = $con->server_version;
   print("MySQL Server Version Number: ".$version);

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

This will produce following result −

MySQL Server Version Number: 50712

Example

<?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_server_version($con);
      print("MySQL Server Version Number: ".$info);
   }
?>

This will produce following result −

Connection Established Successfully
MySQL Server Version Number: 50712

Example

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

This will produce following result −

50712
php_function_reference.htm
Advertisements