PHP Tutorial - 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// ww w  .  ja v  a 2s  .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// w w  w  .  j  av  a  2  s . 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();
?>