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

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP mysqli_ping() Function Usage and Example

PHP MySQLi Reference Manual

The mysqli_ping() function performs a server connection, and if the connection is disconnected, it will attempt to reconnect.

Definition and Usage

mysqli_ping()The function accepts a connection object as a parameter, verifies the connection, and if the connection is disconnected, it will reconnect to the server.

Syntax

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

Parameter

Serial NumberParameters and Description
1

con(Optional)

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

Return value

This function returns a boolean value, true if the operation is successful, false if it fails.

PHP version

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

Online Example

The following examples demonstratemysqli_ping()Usage of the function (procedural style), to check server connection:

<?php
   //Establish connection
   $con = mysqli_connect("localhost","root","password","test");
   $res = mysqli_ping($con);
   if($res){
      print("Successful.....");
   }else{
      print("Failed......");
   }
?>

Output result

Successful.....

Online Example

In the object-oriented style, the syntax of this function is$con-> ping();。The following is an example of this function detecting server connection in an object-oriented style;

<?php
   //Establish connection
   $con = new mysqli("localhost","root","password","test");
   $res = $con->ping();
   if($res){
      print("Successful.....");
   }else{
      print("Failed......");
   }
?>

Output result

Successful.....

Online Example

In the object-oriented style, the syntax of this function is$con-> ping();。The following is an example of this function in an object-oriented style;

<?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 result

Connection is ok!
Connection was successful

PHP MySQLi Reference Manual