Skip to content

Instantly share code, notes, and snippets.

@girvan
Created August 5, 2015 02:14
Show Gist options
  • Save girvan/be3fabb24cf274c4e290 to your computer and use it in GitHub Desktop.
Save girvan/be3fabb24cf274c4e290 to your computer and use it in GitHub Desktop.
<?php
$mysql_checker = function(){
$cfg = explode("\t", DB_CFG);
error_reporting(E_ERROR);
$try_num = 20;
$try_interval = 500000; // 0.5 sec
foreach(range(0, $try_num) as $times)
{
$mysqli = new mysqli($cfg[2], $cfg[0], $cfg[1], $cfg[3]);
// connect fail
if($mysqli->connect_error) {
if($times === ($try_num - 1))
{
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
echo file_get_contents(ROOT_PATH . '/static/500.html');
exit;
}
usleep($try_interval);
continue;
}
// connected
$mysqli->close();
}
};
$mysql_checker();
unset($mysql_checker);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment