I just tried a whole lot a things to do something really simple. I would like to test if a mysqli database connection is alive but in maximum 5 seconds. Just to check: "Hey, are you alive? Good. Continue with the code as quickly as possible."
So far MYSQLI_OPT_CONNECT_TIMEOUT
has not worked for me. The server keeps waiting for +30 seconds and finally throws an error because it had to wait so long. I never intended to have it wait so long. Just check in max 5 seconds if the database is there. Otherwise return false.
This is what I got so far. Again, it waits way too long. The setting seems to be ignored about waiting only 2 seconds.
private function _is_db_connection_alive() {
ini_set( 'mysql.connect_timeout', 2 );
$link = mysqli_init();
$link->options( MYSQLI_OPT_CONNECT_TIMEOUT, 2 );
$result = @$link->real_connect( GA_DB_HOST, GA_DB_USER, GA_DB_PASSWORD, GA_DB_NAME );
return $result;
}
So, how should this 'testing of the connection' be programmed? Thanks.