PHP mysqli_ping() Function

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/*from   w  w w .j a v a2 s .  c  o  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   w w  w. java  2s.c  o  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();
?>




















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions