PHP mysqli_ping() Function
In this chapter you will learn:
- Definition for PHP mysqli_ping() Function
- Syntax for PHP mysqli_ping() Function
- Parameter for PHP mysqli_ping() Function
- Return for PHP mysqli_ping() Function
- Example - pings a server connection
- Example - Object oriented style
Definition
The mysqli_ping() function pings a server connection, and tries to connect if the connection is lost.
Syntax
PHP mysqli_ping() Function has the following syntax.
Object oriented style
bool mysqli::ping ( void )
Procedural style
bool mysqli_ping ( mysqli $link )
Parameter
- link - Procedural style only: A link identifier returned by mysqli_connect() or mysqli_init()
Return
It returns TRUE on success and FALSE on failure.
Example
The following code pings a server connection.
<?php/* j a v a 2 s. co m*/
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno($con)){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (mysqli_ping($con)){
echo "Connection is ok!";
}else{
echo "Error: ". mysqli_error($con);
}
mysqli_close($con);
?>
Example 2
<?php/*from jav a 2 s . co m*/
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
/* check if server is alive */
if ($mysqli->ping()) {
printf ("Our connection is ok!\n");
} else {
printf ("Error: %s\n", $mysqli->error);
}
$mysqli->close();
?>
Next chapter...
What you will learn in the next chapter:
- Definition for PHP mysqli_query() Function
- Syntax for PHP mysqli_query() Function
- Parameter for PHP mysqli_query() Function
- Return for PHP mysqli_query() Function
- Example - performs a query against the database
Home » PHP Tutorial » PHP MySQLi Functions