Skip to content

Instantly share code, notes, and snippets.

@chobie
Created August 24, 2010 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chobie/546876 to your computer and use it in GitHub Desktop.
Save chobie/546876 to your computer and use it in GitHub Desktop.
<?php
/*
mysqliでコネクションを確認しつつ接続しなおすサンプル
*/
$mysqli = mysqli_init();
$mysqli->options(MYSQLI_INIT_COMMAND,"set names utf8");
$host = "localhost";
$user = "user";
$pass = "";
$db_name = "moe";
$mysqli->real_connect($host,$user,$pass,$db_name);
$mysqli->query("SET SESSION wait_timeout = 3");
if($error = mysqli_connect_error()){
throw new Exception($error);
}
$mysqli->set_charset("utf8");
for(;;){
if($mysqli->ping()){
printf ("Our connection is ok!\n");
printf("thread_id:%d\n",$mysqli->thread_id);
sleep(4);
}else{
$stmt = $mysqli->query("select 1");
printf("thread_id:%d\n",$mysqli->thread_id);
printf("Our connection is not good.\n");
$mysqli->real_connect("localhost","websrv","","auction_motekawa");
sleep(3);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment