php 数据查询与连接类

网络整理 - 08-16

php 数据查询与连接类

<?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;
  }
 }
}

?>