读书人

频繁出现Can#039;t connect to MySQL serv

发布时间: 2012-02-09 18:22:27 作者: rapoo

频繁出现Can't connect to MySQL server on 'localhost' (10061)
我的数据库连接代码是
public function open()
{
$db = new Data_Mysql();
return $db;
}

function __construct($pconnect = 0)
{
try {

$func = $pconnect==0 ? 'mysql_connect' : 'mysql_pconnect';

$this->link = @$func($this->dbhost, $this->dbuser, $this->dbpw, 1);

$charset1 = $this->dbcharset == '' ? $this->charset : "GB2312";

if(!empty($charset1))
{
if(function_exists('mysql_set_charset'))
{
@mysql_set_charset($charset1, $this->link);
}
else
{
$collation_query = "SET NAMES '{$charset1}'";
$this->Query($collation_query);
}
}

$this->dbname && @mysql_select_db($this->dbname, $this->link);

} catch (Exception $e) {

if ($this->errno())
{
throw new Common_MyException("Can't connect to database./数据库服务器连接失败 ");
}
}

}

function Query($sql)//执行代码
{
try
{
if(!($result = @mysql_query($sql, $this->link)))
{
throw new Common_MyException($this->error());
}
}
catch (Common_MyException $e)
{
die($e->showStackTrace());
}
return $result;

}

function __destruct()//断开连接
{
if (!$this->errno())
{
$this->close();
}
}
//释放结果集所占用的内存
function free_result($query) {
return mysql_free_result($query);
}

//返回多条记录
public function FetchAll($sql)
{
$returnRows=array();
try
{
if($sql==null) {

throw new MyException("查询语句为空.");
}
if(!($result = $this->Query($sql)))
{
throw new Common_MyException($this->error());
}
while ($rows=$this->fetch_array($result))
{
$returnRows[]=$rows;
}
return $returnRows;

}
catch (Common_MyException $e)
{
die($e->showStackTrace());
}
}

function FetchRow($sql)//返回单条记录
{
$rows=null;

try
{
if($sql==null || $sql=="") {
throw new MyException("查询语句为空.");
}

if(!($result = $this->Query($sql)))
{
throw new Common_MyException($this->error());
}
$rows = $this->fetch_array($result);

}
catch (Common_MyException $e)
{
die($e->showStackTrace());
}
return $rows;
}

function error() {
return (($this->link) ? mysql_error($this->link) : mysql_error());
}

function errno() {
return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());


}

function select_db($dbname) {
return mysql_select_db($dbname, $this->link);
}

function close() {
return mysql_close($this->link);
}

function num_rows($query) {
$query = mysql_num_rows($query);
return $query;
}

function result($query, $row = 0) {
$query = @mysql_result($query, $row);
return $query;
}

function insert_id() {
return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
}

function fetch_array($query, $result_type = MYSQL_ASSOC) {
return mysql_fetch_array($query, $result_type);
}

可是网站都是频繁报错
Warning: mysql_set_charset() expects parameter 2 to be resource, boolean given in D:\webSite\wwwz2gcomcn\Inc\Data\Mysql.php on line 37

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in D:\webSite\wwwz2gcomcn\Inc\Data\Mysql.php on line 62
或者
Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in D:\webSite\wwwz2gcomcn\Inc\Data\Mysql.php on line 151
哪位高手能帮我解决下呀,小弟在这不胜感激


[解决办法]

引用检查一下你的MYSQL错误日志,看看其中是什么信息。

读书人网 >Mysql

热点推荐