function reconnect(){ if (!mysql_ping ($this->db)) { //here is the major trick, you have to close the connection (even though its not currently working) for it to recreate properly. mysql_close($this->db); $this->connect(); } }
复制代码
其中的mysql_ping()用来判断连接是否已经被断开了,若是断开了,关闭当前的链接,重新创建新的连接。
这样,只要发现连接被断开了,即可重新连接了。