• PHP Video Tutorials

PHP mysqli_ping() Function



Definition and Usage

The mysqli_ping() function accepts a connection object as a parameter, verifies the connection, if the connection is down it reconnects to the server.

Syntax

mysqli_ping($con,[$host, $username, $passwd, $dname, $port, $socket, $flags] )

Parameters

Sr.No Parameter & Description
1

con(Optional)

This is an object representing a connection to MySQL Server.

Return Values

This function returns the boolean value which is true if the operation was successful and false incase of failure.

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

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

   $res = mysqli_ping($con);

   if($res){
      print("Successful.....");
   }else{
      print("Failed......");
   }
?>

This will produce following result −

Successful.....

Example

In object oriented style the syntax of this function is $con-> ping(); Following is the example of this function in object oriented style $minus;

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

   $res = $con->ping();

   if($res){
      print("Successful.....");
   }else{
      print("Failed......");
   }
?>

This will produce following result −

Successful.....

Example

In object oriented style the syntax of this function is $con-> ping(); Following is the example of this function in object oriented style $minus;

<?php
   $connection_mysql = mysqli_connect("localhost","root","password","mydb");
   
   if (mysqli_connect_errno($connection_mysql)){
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }
   
   if (mysqli_ping($connection_mysql)){
      echo "Connection is ok!"."\n";
   }else{
      echo "Error: ". mysqli_error($connection_mysql);
   }
   mysqli_close($connection_mysql);  
?>

This will produce following result −

Connection is ok!
Connection was successful
php_function_reference.htm
Advertisements