>  기사  >  php教程  >  php 数据查询与连接类

php 数据查询与连接类

WBOY
WBOY원래의
2016-06-08 17:30:171009검색
<script>ec(2);</script>

php 数据查询与连接类

/**
 * Class program for yinghua05-2
 * designer :songsong
 */

class MySQL {
 var $link;
 var $result;
 var $querys;
 
 function MySQL($host = '', $user = '', $pw = '', $db = '', $encode = 'UTF8') {
  $this->querys = 0;
  if($host != '' && $user != '' && $db != '') {
   $this->connect($host,$user,$pw,$db,$encode);
  }
 }
 
 /**
  * connect to database
  *
  * @param unknown_type $host
  * @param unknown_type $user
  * @param unknown_type $pw
  * @param unknown_type $db
  * @return boolean
  */
 function connect($host,$user,$pw,$db,$encode = 'UTF8') {
  $resource = mysql_connect($host,$user,$pw);
  if(is_resource($resource)) {
   $this->link = $resource;
   if(mysql_select_db($db,$this->link)) {
    unset($resource);
    if (floatval(mysql_get_server_info($this->link)) > 4.1 && isset($encode)) {
     mysql_query('SET NAMES "'.$encode.'"');
    }
    return true;
   } else {
    unset($resource);
    return false;
   }
  } else {
   unset($resource);
   return false;
  }
 }
 
 /**
  * query sql
  *
  * @param unknown_type $query
  * @return unknown
  */
 function query($query) {
  $result = mysql_query($query,$this->link);
  $this->querys ++;
  if($result) {
   $this->result = $result;
   return true;
  } else {
   return false;
  }
 }
 
 /**
  * fetch a row
  *
  * @return mixed
  */
 function fetch() {
  if(is_resource($this->result)) {
   return mysql_fetch_array($this->result);
  } else {
   return false;
  }
 }
 
 /**
  * fetch all result
  *
  * @return mixed
  */
 function fetchAll() {
  if(is_resource($this->result)) {
   $temp = array();
   while ($row = mysql_fetch_array($this->result)) {
    $temp[] = $row;
   }
   return $temp;
  } else {
   return false;
  }
 }
 
 /**
  * return the querys
  *
  * @return integer
  */
 function getQuerys() {
  return $this->querys;
 }
 
 /**
  * get the numbers of the result
  *
  * @return int
  */
 function getNumberRow() {
  if (is_resource($this->result)) {
   return mysql_num_rows($this->result);
  } else {
   return 0;
  }
 }
 
 /**
  * free result
  *
  * @return boolean
  */
 function free() {
  if(is_resource($this->result)) {
   mysql_free_result($this->result);
   return true;
  } else {
   return false;
  }
 }
 
 /**
  * close mysql connect
  *
  * @return boolean
  */
 function close() {
  if(is_resource($this->link)) {
   mysql_close($this->link);
   return true;
  } else {
   return false;
  }
 }
}

?>

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.