This does not work with mysqlnd and is marked as wontfix: https://bugs.php.net/bug.php?id=52561
(PHP 5, PHP 7, PHP 8)
mysqli::ping -- mysqli_ping — サーバーとの接続をチェックし、もし切断されている場合は再接続を試みる
この関数は PHP 8.4.0 で 非推奨になります。この関数に頼らないことを強く推奨します。
オブジェクト指向型
手続き型
サーバーとの接続が動作中かどうかを確かめます。もし切断されており、 グローバルオプション mysqli.reconnect が有効な場合は再接続を試みます。
注意: mysqlnd ドライバは、php.ini 設定項目 mysqli.reconnect を無視します。 自動再接続は行われません。
この関数は、長期間アイドル状態にあるクライアントが、 サーバーの状態を確認して必要なら再接続するために使用します。
mysqli のエラー報告 (MYSQLI_REPORT_ERROR
) が有効になっており、かつ要求された操作が失敗した場合は、警告が発生します。さらに、エラー報告のモードが MYSQLI_REPORT_STRICT
に設定されていた場合は、mysqli_sql_exception が代わりにスローされます。
バージョン | 説明 |
---|---|
8.4.0 |
mysqli::ping() と
mysqli_ping() は非推奨となりました。
reconnect 機能は
PHP 8.2.0 で廃止されたため、この関数はもはや必要ありません。
|
例1 mysqli::ping() の例
オブジェクト指向型
<?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();
?>
手続き型
<?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!
This does not work with mysqlnd and is marked as wontfix: https://bugs.php.net/bug.php?id=52561
As jay at grooveshark dot com very helpfully pointed out, the mysqlnd driver which is becoming pretty standard does not obey reconnect commands. If you have a DB wrapper class (which hopefully you do) you can implement your own version of ping() such as:<?phpclass db extends mysqli{ private $db_host; private $db_user; private $db_pass; private $db_name; private $persistent; public function __construct($db_host, $db_user, $db_pass, $db_name, $persistent = true) { $this->db_host = $db_host; $this->db_user = $db_user; $this->db_pass = $db_pass; $this->db_name = $db_name; $this->persistent = $persistent; parent::init(); parent::options(MYSQLI_OPT_CONNECT_TIMEOUT, 1); @parent::real_connect(($this->persistent ? 'p:' : '') . $this->db_host, $this->db_user, $this->db_pass, $this->db_name); if ($this->connect_errno) die("All DB servers down!\n"); } public function ping() { @parent::query('SELECT LAST_INSERT_ID()'); if ($this->errno == 2006) $this->__construct($this->db_host, $this->db_user, $this->db_pass, $this->db_name, $this->persistent); }...}$db = new db(DB_HOST, DB_USER, DB_PASS, DB_NAME);// Some code that potentially takes a really long time to execute goes here// Ping for safety to try to gracefully reconnect$db->ping();// Now we should be able to run queries again$db->query('SELECT LAST_INSERT_ID()');?>If you wanted you could even put "$this->ping();" at the top of db::query() to avoid any explicit reconnection calls but I wouldn't recommend it due to the (slight) overhead of running the cheap "SELECT LAST_INSERT_ID()" query every time prior to running your real intended query. There are probably even cheaper queries to run in favor of "SELECT LAST_INSERT_ID()" but it was the first that came to mind and is cheap enough for most purposes since you shouldn't be calling ping() a whole bunch anyway.
The behaviour about the option mysqli.reconnect is default set to Off at Debian PHP Packages. So i would recommend to update the first line description about the recommendation at the option mysqli.reconnect. (practice note ;))