PHP长时间连接数据库造成mysql has gone away解决办法
程序员文章站
2024-02-06 21:33:10
...
PHP长时间连接数据库造成mysql has gone away
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");
}
上面贴的是数据库连接的写法,请问怎么写能做到不出现错误呢?还有其他的方法吗?
------解决方案--------------------
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("Mysql_Error : ".mysql_error()."
Mysql Error Num : ".mysql_errno()."");
}
@mysql_select_db($db_name, $connect) or die(" Mysql_Error : ".mysql_error()."
Mysql Error Num : ".mysql_errno()."");
return $connect;
}
相关文章
相关视频