English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The mysqli_ping() function performs a server connection, and if the connection is disconnected, it will attempt to reconnect.
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.
mysqli_ping($con, [$host, $username, $passwd, $dname, $port, $socket, $flags])
Serial Number | Parameters and Description |
---|---|
1 | con(Optional) This is an object representing the connection to the MySQL Server. |
This function returns a boolean value, true if the operation is successful, false if it fails.
This function was originally introduced in PHP version5introduced and can be used in all higher versions.
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.....
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.....
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