You can call the mysql_ping() function to check whether the connection to the server is working.
Syntax
int
mysql_ping(MYSQL *mysql)
Return values
0 is returned if the connection to the server is active, and a non-zero value is returned if an error occurred.
A non-zero value does not indicate whether the MySQL server is down. The connection might be broken due to other reasons such as network problems.
Errors
CR_COMMANDS_OUT_OF_SYNC: Commands were executed in an improper order.CR_SERVER_GONE_ERROR: The connection to the OBServer was disconnected.CR_UNKNOWN_ERROR: An unknown error occurred.
Notes
If the connection is closed and automatic reconnection is enabled, an attempt to reconnect is made. If the connection is closed and automatic reconnection is disabled, mysql_ping() returns an error.
Automatic reconnection is disabled by default. To enable it, call mysql_options() with the MYSQL_OPT_RECONNECT option.
mysql_ping() can be used by clients that remain idle for a long time, to check whether the server has closed the connection and reconnect to the server if necessary.
mysql_ping() automatically causes a reconnection with no explicit indication. If a reconnection occurs, some characteristics of the connection will be reset. To determine whether a reconnection occurs, call mysql_thread_id() to get the original connection identifier before calling mysql_ping(), and then call mysql_thread_id() again to check whether the identifier has changed.