PHP长时间连接数据库造成mysql has gone away
程序员文章站
2024-01-10 14:50:26
...
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");
}
上面贴的是数据库连接的写法,请问怎么写能做到不出现错误呢?还有其他的方法吗?
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;}
建议查询完毕,关闭连接数据库
推荐阅读
-
PHP长时间连接数据库造成mysql has gone away
-
PHP长时间连接数据库造成mysql has gone away解决办法
-
PHP长时间连接数据库促成mysql has gone away
-
数据库还原提示MySQL server has gone away 的解决方法
-
mysql 远程连接出现MySQL server has gone away的解决办法
-
PHP程序mysql报错mysql has gone away
-
php用pdo的like查询语句,间接性报错 MySQL server has gone away
-
远程连接mysql 这几天一直出现 2006 'MySQL server has gone away'
-
PHP程序mysql报错mysql has gone away
-
php用pdo的like查询语句,间接性报错 MySQL server has gone away