Current Location: Home> Function Categories> mysqli::ping

mysqli::ping

(mysqli_ping)ping server connection, or try to reconnect if the connection is disconnected
Name:mysqli::ping
Category:MySQLi
Programming Language:php
One-line Description:ping the server connection, or try to reconnect when the connection is disconnected.

Definition and usage

The ping() / mysqli_ping() function is used to ping the server connection to check whether the server is alive. If the connection is disconnected, it will also try to reconnect.

Example

Example 1 - Object-Oriented Style

ping server connection:

 <?php
$mysqli = new mysqli ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

// Check the connection
if ( $mysqli -> connect_errno ) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error ;
  exit ( ) ;
}

// Check whether the server is alive
if ( $mysqli -> ping ( ) ) {
  echo "Connection is ok!" ;
} else {
  echo "Error: " . $mysqli -> error ;
}

$mysqli -> close ( ) ;
?>

Example 2 - Procedural Style

ping server connection:

 <?php
$con = mysqli_connect ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

// Check the connection
if ( mysqli_connect_errno ( ) ) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error ( ) ;
  exit ( ) ;
}

// Check whether the server is alive
if ( mysqli_ping ( $con ) ) {
  echo "Connection is ok!" ;
} else {
  echo "Error: " . mysqli_error ( $con ) ;
}

mysqli_close ( $con ) ;
?>
Similar Functions
Popular Articles