I have some websites that uses MySQL. I'm not expert in SQL, so I use simple connection, querys, etc.. Sometimes (is rare but happens) only the database server hangs, or I forgot to turn on my home testing mysql. Until it operational again, the server hangs trying to connect and finally a timeout error happens.
I'm trying to add some previous test of database server like this "ping" function:
function pingDomain($domain){
$starttime = microtime(true);
$file = fsockopen ($domain, 80, $errno, $errstr, 10);
$stoptime = microtime(true);
$status = 0;
if (!$file) {
$status = -1; // Site is down
} else {
fclose($file);
$status = ($stoptime - $starttime) * 1000;
$status = floor($status);
}
return $status;
}
But didn't work either, php hangs anyway. Any ideas?