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() {
        $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);
    }
}


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
博文 247
码字总数 304294
×
mac_zhao
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: