PHP长时间连接数据库造成mysql has gone away

WBOY
Release: 2016-06-23 14:21:21
Original
1013 people have browsed it

public function connect() {
if ($this->conn == "pconn") {
//永久链接
$this->conn = mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd);
} else {
//即使链接
$this->conn = mysql_connect($this->db_host, $this->db_user, $this->db_pwd);
}

if (!mysql_select_db($this->db_database, $this->conn)) {
if ($this->show_error) {
$this->show_error("数据库不可用:", $this->db_database);
}
}
mysql_query("SET NAMES $this->coding");
}

上面贴的是数据库连接的写法,请问怎么写能做到不出现错误呢?还有其他的方法吗?


回复讨论(解决方案)

求高手给个方法

数据库链接超时或超出无操作超时设定被数据库强制断开链接;另一个可能就是数据库不稳定。不建议持久连接~~也不建议一次获取超大的数据结果~

function dbConnect($hostname,$username,$pass,$db_name,$pconnect = 0) {    $func = empty($pconnect) ? 'mysql_connect' : 'mysql_pconnect';        if(!$connect) {        $connect = @$func($hostname,$username,$pass) or die("<font size='2'>Mysql_Error : ".mysql_error()."<br>Mysql Error Num : ".mysql_errno()."</font>");    }        @mysql_select_db($db_name, $connect) or die("<font size='2'> Mysql_Error : ".mysql_error()."<br>Mysql Error Num : ".mysql_errno()."</font>");    return $connect;}
Copy after login

建议查询完毕,关闭连接数据库

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template