Ping a mysql server connection in PHP

Description

The following code shows how to ping a mysql server connection.

Example


  /*from   ww  w .j a  v  a 2s.  com*/
<?php
    $con=mysqli_connect("localhost","root","","test");
    
    // Check connection
    if (mysqli_connect_errno()){
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    // Check if server is alive
    if (mysqli_ping($con)){
       echo "Connection is ok!";
    }else{
       echo "Error: ". mysqli_error($con);
    }
   
    mysqli_close($con);
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    MySQL »




MySQLi
MySQLi Object Oriented