首页 > 编程技术 > php

php 数据查询与连接类

发布时间:2016-11-25 16:04

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

?>

header("Content-Type: $mime");
header("Content-Disposition: attachment; filename="$file"");
header("Content-Length: ".filesize($filename));
if(strstr($mime, "text")){
    $file_handle = @fopen("$filename","r");
}else{
    $file_handle = @fopen("$filename","rb");
}

php 实现文本文件下载

php类的使用实例教程

<?php
/**
 * Class program for yinghua05-2
 * designer :songsong
 */

class Template {
 var $tpl_vars;
 var $tpl_path;
 var $_debug;
 
 /**
  * Construct for Template
  * PHP5 or upper version
  */
 function __construct() {
  $this->Template();
 }
 
 /**
  * Construct for Template
  *
  * @return Template
  */
 function Template() {
  $this->tpl_vars = array();
  $this->tpl_path = '';
  $this->_debug = false;
 }
 
 /**
  * Set template path
  *
  * @param string $path
  * @return boolean
  */
 function setPath($path) {
  if(is_dir($path)) {
   $path = rtrim($path,'/').'/';
   $this->tpl_path = $path;
   return true;
  } else {
   if($this->_debug) {
    $this->_debug('template path is not exists.');
   }
   return false;
  }
 }
 
 /**
  * Enter description here...
  *
  * @param mixed $var
  * @param mixed $val
  */
 function assign($var,$val) {
  if(isset($var) && is_array($var)) {
   $this->tpl_vars = $var;
  } else if(isset($var) && $var != '') {
   $this->tpl_vars[$var] = $val;
  } else {
   if($this->_debug == true) {
    $this->_debug('set variable error.');
   }
   return false;
  }
 }
 
 /**
  * Display template file
  *
  * @param String $file_name
  */
 function display($file_name) {
  ob_start();
  extract($this->tpl_vars);
  $include_flie = $this->tpl_path . $file_name;
  if(!file_exists($include_flie)) {
   if($this->_debug)
    $this->_debug('Template file "'.$include_flie.'" is not exists.');
   else
    exit('Template error, please check it.');
  }
  include($include_flie);
  $content = ob_get_contents();
  ob_end_clean();
  
  echo $content;
 }
 
 /**
  * Debuging
  *
  */
 function _debug($msg = '') {
  die('Error :'.$msg);
 }
}

?>

php常用文件上传类

<?php
/**
 * flie class
 * (jpg,gif,png)
 */
class Upload {
 var $_file;
 var $_fileType;
 var $target = 'upload/';
 
 /**
  * construct for this class
  *
  * @param string $name
  * @return Upload
  */
 function Upload($name) {
  if(isset($_FILES[$name])) {
   $this->_file = &$_FILES[$name];
   $this->_parseUploadFile();
  } else {
   die('No file upload.');
  }
 }
 
 /**
  * set upload target path
  *
  * @param string $path
  * @return boolean
  */
 function setTarget($path = 'upload/') {
  if(is_dir($path)) {
   $this->target = rtrim($path,'/').'/';
   return true;
  } else {
   return false;
  }
 }
 
 /**
  * get the type of the file
  *
  */
 function _parseUploadFile() {
  $type = $this->_file['type'];
  if(isset($type) && $type != '') {
   switch ($type) {
    case 'image/gif':
     $this->_fileType = 'gif';
     break;
    case 'image/png':
     $this->_fileType = 'png';
     break;
    case 'image/jpeg':
     $this->_fileType = 'jpg';
     break;
    case 'image/pjpeg':
     $this->_fileType = 'jpg';
     break;
    default:
     $this->_fileType = 'unknow';
     break;
   }
  } else {
   $filename = $this->_file['name'];
   $filename = explode('.',$filename);
   $filename = strtoupper($filename[sizeof($filename) - 1]);
   switch ($filename) {
    case 'PNG':
     $this->_fileType = 'png';
     break;
    case 'JPEG':
     $this->_fileType = 'jpg';
     break;
    case 'JPG':
     $this->_fileType = 'jpg';
     break;
    case 'GIF':
     $this->_fileType = 'gif';
     break;
    default:
     $this->_fileType = 'unknow';
     break; 
   }
   unset($filename);
  }
  unset($type);
 }
 
 /**
  * upload file
  *
  * @return array
  */
 function load() {
  if($this->_fileType == 'unknow') {
   die('Can not upload this file,because the type is not allow.');
  }
  if(file_exists($this->_file['tmp_name']) && is_uploaded_file($this->_file['tmp_name'])) {
   $new_file_name = $this->target.time().'.'.$this->_fileType;
   move_uploaded_file($this->_file['tmp_name'],$new_file_name);
   
   return array('name'=>$new_file_name,'size'=>$this->_file['size'],'type'=>$this->_fileType);
  } else {
   return false;
  }
 }
}
?>

php mysql查询类<?php
 class Ring{
  var $q_uid;
  var $q_name;
  var $q_type;
  var $q_head;
  var $q_sex;
  var $q_reg;  
  
  public check_user($id){
    
    $sql="Select * from gx_member where id='$id'";
    
    $result=mysql_query($sql) or die(mysql_error());
    
    if (mysql_num_rows( $result ) ){
      
      $rs =mysql_fetch_object( $result);
      
      if( $rs->m_lock=='1' ){
       
        exit('对不起,你己被管理员锁定了');
      
      }elseif( $rs->m_del =='1') {
        
        exit('此用户己被删除');
        
      }else{
    
        $this->q_uid =$id;
        
        $this->q_name=$rs->m_name;
        
        $this->q_head=$rs->m_head
        
        $this->q_sex =$rs->sex == '1'? '男':'女';
        
        $this->q_reg =$rs->m_time;
        
        $this->q_type=$rs->type;
      }
      
    }else{
     
      exit('用户不存了..<a href=# onclick='history.back();'>点击返回</a>');
     
    }
  }
  
  public member_article(){
   
   $article_list ="Select * from gx_q where member_id='$this->q_uid";
   
   if( '0' == $this->query($article_list) ){
     
     echo '暂时无信息';
     
   }else{
     
     foreach( $this->result as $list){
       
       $size =sizeof($list);
       
       for($i=0;$i<=$size;$i++){
       
        /* 222*/
        
       }
      
   }
    
    
  }
  
  public query($sql){
  
    if( empty( $sql) ){
      
      return '0';
    }else{
     
      $result =mysql_query($sql) or die(mysql_error());
      
      if( mysql_num_rows ($result) ){
      
        $array =array();
        
        while( $rs =mysql_fetch_array($result) )
        
         $array[]=$rs;
        
        }
        
        $this->result=$array;
        
      }else{
        
        return '0';
        
      }
      mysql_free_result($result);
    
    }
   
  }
  
 }
?>
注:本站原创转载注明:  www.111cn.net 

标签:[!--infotagslink--]

您可能感兴趣的文章: