mysqli_ping

(PHP 5)

mysqli_ping

(no version information, might be only in CVS)

mysqli->ping --  サーバとの接続をチェックし、もし切断されている場合は再接続を試みる

説明

手続き型:

bool mysqli_ping ( mysqli link )

オブジェクト指向型(メソッド):

class mysqli {

bool ping ( void )

}

サーバとの接続が動作中かどうかを確かめます。もし切断されており、 グローバルオプション mysqli.reconnect が有効な場合は 再接続が試みられます。

この関数は、長期間アイドル状態にあるクライアントが、 サーバの状態を確認して必要なら再接続するために使用します。

返り値

成功した場合に TRUE を、失敗した場合に FALSE を返します。

例 1. オブジェクト指向型

<?php
$mysqli
= new mysqli("localhost", "my_user", "my_password", "world");

/* 接続状況をチェックします */
if (mysqli_connect_errno()) {
    
printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* サーバが稼動中かどうかを確認します */
if ($mysqli->ping()) {
    
printf ("Our connection is ok!\n");
} else {
    
printf ("Error: %s\n", $mysqli->error);
}

/* 接続を閉じます */
$mysqli->close();
?>

例 2. 手続き型

<?php
$link
= mysqli_connect("localhost", "my_user", "my_password", "world");

/* 接続状況をチェックします */
if (mysqli_connect_errno()) {
    
printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* サーバが稼動中かどうかを確認します */
if (mysqli_ping($link)) {
    
printf ("Our connection is ok!\n");
} else {
    
printf ("Error: %s\n", mysqli_error($link));
}

/* 接続を閉じます */
mysqli_close($link);
?>

上の例の出力は以下となります。

Our connection is ok!