PHP | mysqli_ping() Function

PHP mysqli_ping() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP mysqli_ping() Function.

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

Syntax

Following below is the syntax to use this function -

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


Parameter Details

Sr.NoParameter & Description
1

con(Optional)

This is an object representing a connection to MySQL Server.


Return Value

This built-in PHP function returns true if the operation was successful, otherwise false on failure.

PHP Version

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

Example1

The following below is an example which demonstrates the usage of the built-in PHP 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......");
   }
?>

Output

When the above code is executed, it will produce the following result -

Successful.....

Example2

In object oriented style the syntax of this built-in PHP function is $con->ping(); The following is the example of this function in an 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......");
   }
?>

Output

When the above code is executed, it will produce the following result -

Successful.....

Example3

The following below is another example of the built-in PHP mysqli_ping() function in an 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);  
?>

Output

When the above code is executed, it will produce the following result -

Connection is ok!
Connection was successful


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the PHP mysqli_prepare() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.

Thanks for reading and bye for now.