mysql_ping() is really helpful when you have this annoying error:
MYSQL Error 2006 Server has gone away
For CI users:
In 1.7.2 version of codeigniter, there is a function
$this->db->reconnect()
that uses mysql_ping() to reestablish the timed out connection.
This function is specially useful when developing social media sites that uses hundreds of connections to the db such asinserting or selecting.
mysql_ping
(PHP 4 >= 4.3.0, PHP 5)
mysql_ping — サーバーとの接続状況を調べ、接続されていない場合は再接続する
警告
この拡張モジュールは PHP 5.5.0 で非推奨になりました。将来のバージョンで削除される予定です。 MySQLi あるいは PDO_MySQL を使うべきです。詳細な情報は MySQL: API の選択 や それに関連する FAQ を参照ください。 この関数の代替として、これらが使えます。
説明
bool mysql_ping
([ resource
$link_identifier = NULL
] )サーバーとの接続が有効かどうかを調べます。 もし接続が切れていた場合、自動的に再接続が試みられます。 この関数は、アイドル期間が長いスクリプトで利用し、 サーバーが接続を切断したかどうかを確認するために用いられます。
注意:
MySQL 5.0.3 以降、自動再接続機能は使えなくなりました。
パラメータ
-
link_identifier -
MySQL 接続。 指定されない場合、 mysql_connect() により直近にオープンされたリンクが 指定されたと仮定されます。そのようなリンクがない場合、引数を指定せずに mysql_connect() がコールした時と同様にリンクを確立します。 リンクが見付からない、または、確立できない場合、
E_WARNINGレベルのエラーが生成されます。
返り値
MySQL サーバーとの接続が有効な場合に TRUE そうでない場合に
FALSE を返します。
例
例1 mysql_ping() の例
<?php
set_time_limit(0);
$conn = mysql_connect('localhost', 'mysqluser', 'mypass');
$db = mysql_select_db('mydb');
/* このクエリは非常に時間がかかるものと仮定する */
$result = mysql_query($sql);
if (!$result) {
echo 'Query #1 failed, exiting.';
exit;
}
/* 接続が有効かどうかを確かめる。切断されていたら再接続する */
if (!mysql_ping($conn)) {
echo 'Lost connection, exiting after query #1';
exit;
}
mysql_free_result($result);
/* 接続が有効であることが確かめられたので、別のクエリを実行する */
$result2 = mysql_query($sql2);
?>
alext at marketdream dot com dot mx ¶
3 years ago
luky37 ¶
4 years ago
If you get 'error 2006: MySQL server has gone away' messages when running (really) long scripts, mysql_ping will help detecting the loss of the db-connection. This can happen, when 'wait timeout' is reached (MySQL default is 8 hours).
miro dot dietiker at md-systems dot ch ¶
5 years ago
When checking if a $resource works...
be prepared that mysql_ping returns NULL as long as $resource is no correct mysql resource.
<?php
$resource =NULL;
var_dump = @mysql_ping($resource);
# showing NULL
?>
This could be used to decide of a current $resource is a mysql or a mysqli connection when nothing else is available to do that...
dustin hawkins ¶
6 years ago
When using the mysql_ping command under php 5.1.2 and mysql 5.0, I was having problems with the auto-reconnect "feature", mainly that when the connection was severed, a mysql_ping would not automatically re-establish the connection to the database.
The connection to the DB is dropped when the time without a query excedes the wait_timeout value in my.cnf. You can check your wait_timeout by running the query "SHOW VARIABLES;"
If you're having problems auto-reconnecting when the connection is dropped, use this code:
<?php
$conn = mysql_connect('localhost','user','pass');
mysql_select_db('db',$conn);
if (!mysql_ping ($conn)) {
//here is the major trick, you have to close the connection (even though its not currently working) for it to recreate properly.
mysql_close($conn);
$conn = mysql_connect('localhost','user','pass');
mysql_select_db('db',$conn);
}
//run queries knowing that your connection is alive....
?>
cybot2000 at yahoo dot de ¶
7 years ago
It should be noted that mysql_ping() seems to reset the error message on the server.
I used it to check whether the connection was still alive before reading the error message via mysql_error() and it always returned an empty string. Upon removing the connection check everything worked.
Doug ¶
3 years ago
This function *does not* attempt to reconnect at this time. It only tells you whether or not you currently *are* connected.
To actually reconnect, you will have to implement this yourself in a wrapper class.
oscar at bitagenda dot com ¶
5 years ago
Very confusing description.
... If it has gone down, an automatic reconnection is attempted
(But the function returns only true or false, so if you have variable say $link = connection resource, it will be an invalid resource)
Then ...
... Note: Since MySQL 5.0.13, automatic reconnection feature is disabled.
OK, if it returns false, I have the chance to reconnect updating my resource variable
$link = reconnect
But I did called the function with $link to do the check, so ...
If no such link is found, it will try to create one as if mysql_connect() was called with no arguments (this are different arguments than automatic reconnection, doesn't it? I think, automatic reconnecting, say to $link, would be with its own arguments previously used to create the connection)
So bad decision about automatic recconection feature disabled ...
At least the function should have the param by reference, so in case it reconnects with a new link_identifier.
bool mysql_ping ( &[resource $link_identifier] )
Or a return value of link_identifier, not true.
if ($link=mysql_ping($link)) {...} else { whatever }
vinicius at teracom dot com dot br ¶
9 years ago
Is important to remember that if your first connection to mysql don't works, mysql_ping will always return true! So, if you want to check if mysql is connected, first of all you must check if mysql_connect do not returns false and then you can begin to check mysql_ping.
