日期:2014-05-16  浏览次数:20750 次

频繁出现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());
}