mysqli::ping
(mysqli_ping)ping server connection, or try to reconnect if the connection is disconnected
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.
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 ( ) ;
?>
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 ) ;
?>