Terkadang ketika koneksi MySQL Anda dibuka terlalu lama, koneksi ke DB akan terputus ketika waktu tanpa kueri melebihi nilai wait_timeout di my.cnf. Anda akan mendapatkan kesalahan batas waktu "Server MySQL telah hilang".
Beginilah cara saya menerapkan koneksi ulang otomatis dalam kode saya:
class databaseClass {
var $conn;
var $db;
public function __construct() {
$this->connect();
}
public function connect() {
$this->conn = mysql_connect(DB_HOST, DB_USER, DB_PASS);
$this->db = mysql_select_db(DB_NAME, $this->conn);
}
public function disconnect() {
mysql_close($this->conn);
}
public function reconnect() {
$this->disconnect();
$this->connect();
}
public function queryCompanyExist($company) {
//auto reconnect if MySQL server has gone away
if (!mysql_ping($this->conn)) $this->reconnect();
$query = "SELECT name FROM company WHERE name='$company'";
$result = mysql_query($query);
if (!$result) print mysql_error() . "\r\n";
return mysql_fetch_assoc($result);
}
}
Lihat di sini untuk informasi lebih lanjut tentang mysql_ping