int mysql_ping(MYSQL *mysql)
Description
Checks whether the connection to the server is working. If the
connection has gone down and auto-reconnect is enabled an
attempt to reconnect is made. If the connection is down and
auto-reconnect is disabled,
mysql_ping()
returns an error.
Auto-reconnect is disabled by default. To enable it, call
mysql_options()
with the
MYSQL_OPT_RECONNECT
option. For details, see
Section 20.10.3.49, “mysql_options()
”.
mysql_ping()
can be used by
clients that remain idle for a long while, to check whether the
server has closed the connection and reconnect if necessary.
If mysql_ping()
) does cause a
reconnect, there is no explicit indication of it. To determine
whether a reconnect occurs, call
mysql_thread_id()
to get the
original connection identifier before calling
mysql_ping()
, and then call
mysql_thread_id()
again to see
whether the identifier has changed.
If reconnect occurs, some characteristics of the connection will have been reset. For details about these characteristics, see Section 20.10.11, “Controlling Automatic Reconnection Behavior”.
Return Values
Zero if the connection to the server is alive. Nonzero if an error occurred. A nonzero return does not indicate whether the MySQL server itself is down; the connection might be broken for other reasons such as network problems.
Errors
Commands were executed in an improper order.
The MySQL server has gone away.
An unknown error occurred.
User Comments
An important thing to keep in mind, especially if you're using threads to keep a database connection open/persistant:
A call to mysql_ping, even if the connection never went down, will wipe the results of a mysql_affected_rows, if it somehow occurs between a command execution, and the call.
Symptom: mysql_affected_rows returns -1, when you expect a different number.
(Note - there could be other reasons, this is just one way to guarantee this behavior.)
Add your own comment.