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 }
mysql_ping
(PHP 4 >= 4.3.0, PHP 5, PECL mysql:1.0)
mysql_ping — 서버 접속을 검사하고 접속이 없으면 재접속한다
설명
bool mysql_ping
([ resource $link_identifier
] )
서버로의 접속이 정상적으로 동작하는지 점검한다. 접속이 종료되었다면 자동으로 재접속을 시도한다. 이 함수는 스크립트에서 사용된 이후 오랜 시간동안 멈추어 있을 때때, 접속이 종료되지 않았는지 점검하고 필요시에는 재접속을 하는 용도로 사용된다.
매개변수
- link_identifier
-
MySQL 연결. 지정하지 않으면 mysql_connect()로 연 마지막 연결을 사용합니다. 연결이 없으면, 매개변수 없이 mysql_connect()를 호출하여 연결을 만듭니다. 연결이 성립되지 않으면 E_WARNING 경고를 생성합니다.
반환값
MySQL 서버와의 접속이 정상적이면 TRUE를 그렇지 않다면 FALSE를 반환한다.
예제
Example#1 mysql_ping() 예제
<?php
set_time_limit(0);
$conn = mysql_connect('localhost', 'mysqluser', 'mypass');
$db = mysql_select_db('mydb');
/* Assuming this query will take a long time */
$result = mysql_query($sql);
if (!$result) {
echo 'Query #1 failed, exiting.';
exit;
}
/* Make sure the connection is still alive, if not, try to reconnect */
if (!mysql_ping($conn)) {
echo 'Lost connection, exiting after query #1';
exit;
}
mysql_free_result($result);
/* So the connection is still alive, let's run another query */
$result2 = mysql_query($sql2);
?>
mysql_ping
oscar at bitagenda dot com
02-Oct-2007 04:10
02-Oct-2007 04:10
miro dot dietiker at md-systems dot ch
31-Aug-2007 11:32
31-Aug-2007 11:32
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
31-Jul-2006 12:32
31-Jul-2006 12:32
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
25-May-2005 04:44
25-May-2005 04:44
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.
vinicius at teracom dot com dot br
16-Mar-2004 07:35
16-Mar-2004 07:35
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.
