The mysqli_ping() function is used to ping a server connection. That is it is used to check if a host is reachable on an IP network or not. This function also tries to reconnect if an existing server connection is lost. To use this function, it is mandatory to first set up the connection with the MySQL database.
This function can be used in both Object Oriented and Procedural styles as described below:
- Object oriented style:
Syntax:
ping();
Parameters: This function does not accepts any parameter, it is used with a connection instance.
Return Value: This function returns True on success and False on faliure.
Below program illustrate the ping() function in object-oriented style:
<?php$servername="localhost";$username="username";$password="password";// Creating a connection$conn=newmysqli($servername,$username,$password);// Check connectionif($conn->connect_error) {die("Connection to the server failed: ".$conn->connect_error);}/* check if server is alive */if($conn->ping()) {printf ("Successful Coonection!\n");}else{printf ("Error: %s\n",$conn->error);}/* close connection */$conn->close();?>chevron_rightfilter_none - Procedural style:
Syntax:
mysqli_ping($conn);
Parameters: This function accepts a single parameter $conn which represents the connection to use.
Return Value: This function returns True on success and False on faliure.
Below program illustrate the mysqli_ping() in the procedural style:
<?php$$servername="localhost";$username="username";$password="password";// Creating connection$conn= mysqli_connect($servername,$username,$password);// Checking connectionif(!$conn) {die("Connection to the server failed: ". mysqli_connect_error());}/* check if server is alive */if(mysqli_ping($conn)) {printf ("Successful Coonection!\n");}else{printf ("Error: %s\n", mysqli_error($conn));}/* close connection */mysqli_close($conn);?>chevron_rightfilter_none
Reference: http://php.net/manual/en/mysqli.ping.php
Recommended Posts:
- How to Check a Function is a Generator Function or not using JavaScript ?
- How to call a function that return another function in JavaScript ?
- How to get the function name inside a function in PHP ?
- How to get the function name from within that function using JavaScript ?
- PHP | key() Function
- CSS min() Function
- PHP | Ds\Map get() Function
- PHP | pos() Function
- PHP | end() Function
- PHP | Ds\Set xor() Function
- D3.js | d3.map.set() Function
- PHP | each() Function
- p5.js | value() Function
- PHP | Ds\Set first() Function
- p5.js | mag() Function
- CSS | url() Function
- PHP | Ds\Map put() Function
- PHP | ord() Function
- PHP | Ds\Map xor() Function
- D3.js | d3.map.has() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

