MySQL server has gone away
博客专区 > mac_zhao 的博客 > 博客详情
MySQL server has gone away
mac_zhao 发表于2年前
MySQL server has gone away
  • 发表于 2年前
  • 阅读 14
  • 收藏 0
  • 点赞 1
  • 评论 0


摘要: MySQL server has gone away
class databaseClass {
    var $conn;
    var $db;

    public function __construct() {

    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() {

    public function reconnect() {

    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);

mysqli reconnect=true

You can write a function which will ping the database via connection and in case it down, reconnect it again and then proceed the query or whatever you want, also take a look on mysqli php library, it can be useful for you.

PS. Also could be useful to implement Singleton design pattern in order to maintain the database connection, once created it will connect to database and then you can implement the method called getConnection which each time will proceed with the check I've described above.

PPS. You can use an exception, try you query, whenever it fails catch an exception, reconnect and try again.

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 40
博文 248
码字总数 304334
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
* 支付类型