现在的位置: 首页 > 综合 > 正文

一个php操作数据库的封装类,超实用.

2018年01月10日 ⁄ 综合 ⁄ 共 3820字 ⁄ 字号 评论关闭

详情请点击 断桥残雪

 

<?php
 
 
 class SqlHelper{
  
  
  private $dbname="";
  private $userName="";
  public  $password="";
  private $conn;
  private $host="";
   
  /**
   * @author:lixiuran
   * 初始化数据库的配置信息
   * Enter description here ...
   */
  public function configInit(){
   $filename="../config/db_info.ini";
   $db_infos=parse_ini_file($filename);
   
   for($i=0;$i<count($db_infos);$i++){
    $this->host= $db_infos["host"];
    $this->userName=$db_infos["user_name"];
    $this->dbname=$db_infos["db_name"];
    $this->password=$db_infos["password"];
   }
  }
  /*
   * 该类的构造放发,也可用__construct
   * 初始化连接,选择的数据库,以及编码方式。
   */
  public function  SqlHelper(){
   $this->configInit();
   
   $this->conn=mysql_connect($this->host,$this->userName,$this->password);
   if(!$this->conn){
    echo "数据库连接失败".mysql_error();
   }
   $db=mysql_select_db($this->dbname,$this->conn);
   if(!$db){
    echo "数据库选择失败".mysql_error();
   }
   mysql_query("set character set gbk") or die("编码设置错误".mysql_error());
   mysql_query("SET NAMES 'GBK'") or die("编码设置错误".mysql_error());
  }
  /*
   * 执行查询语句,并返回一个二维数组
   */
  public function execute_dql($sql){
   $arr=array();
   $result=mysql_query($sql,$this->conn) or die(mysql_error());
   while($row=mysql_fetch_assoc($result)){
    $arr[]=$row;
   }
   mysql_free_result($result);
   return $arr;
  }
  /**
   * $sql_fenye_page:分页查询的sql语句
   * $sql_total_count:查询表的总记录数。
   *
   * Enter description here ...
   * @param unknown_type $sql_fenye_page
   * @param unknown_type $sql_total_count
   * @param unknown_type $fenye
   */
  public function execute_dql_fenye($sql_fenye_page,$sql_total_count,$fenye){
    $result=mysql_query($sql_fenye_page,$this->conn) or die(mysql_error());
    $arr=array();
    while($row=mysql_fetch_assoc($result)){
     $arr[]=$row;
    }
    mysql_free_result($result);
    $fenye->res_arr=$arr;
    //处理总记录数及页数
    $result2=mysql_query($sql_total_count) or die(mysql_error());
    if($row=mysql_fetch_row($result2)){
     $fenye->pageCount=ceil($row[0]/$fenye->pageSize);
     $fenye->rowCount=$row[0];
     
    }
   
    //导航条*************************************************************************
     $nav=""; 
    
      $pageWhole=5;
     $start=floor(($fenye->pageNow-1)/$pageWhole)*$pageWhole+1;
     $index=$start;
     
         $nav.="<a href={$fenye->goUrl}?pageNow=1>首页&nbsp;</a>";
        if($fenye->pageNow>1){
         $nav.="<a href={$fenye->goUrl}?pageNow=".($fenye->pageNow-1).">上一页&nbsp;</a>";
        }
        //循环打印草链接
     if($fenye->pageNow>$pageWhole){
      $nav.="<a href={$fenye->goUrl}?pageNow=".($start-1).">&nbsp;<<</a>";
     }
     for(;$start<$index+$pageWhole;$start++){
      if($start<=$fenye->pageCount){
       $nav.="&nbsp;<a href={$fenye->goUrl}?pageNow=".$start.">[".$start."]</a>";
      }
     }
     if($fenye->pageNow<($fenye->pageCount-$pageWhole)){
      $nav.="&nbsp;<a href={$fenye->goUrl}?pageNow=".$start.">&nbsp;>></a>";
     }
        if($fenye->pageNow<$fenye->pageCount){
         $nav.="<a href={$fenye->goUrl}?pageNow=".($fenye->pageNow+1).">&nbsp;下一页</a>";
        }
         $nav.="<a href={$fenye->goUrl}?pageNow=$fenye->pageCount>&nbsp;尾页</a>";
         $nav.="当前$fenye->pageNow/{$fenye->pageCount}页|共{$fenye->rowCount}条";
         
     
     $nav.= "<form action='{$fenye->goUrl}' method='get' onsubmit='return checkGO()'>";
     $nav.="<input type='text' size='4' id='pageNow' name='pageNow'/>";
     $nav.=" <input type='submit' value='GO'/>";
     $nav.="</form>";
     
     $fenye->navigator=$nav;
     //*************************************************************************
    mysql_free_result($result2);
    
    return $fenye;
  }
  /*
   * 执行 crud 操作,并返回状态玛。
   */
  public function execute_dml($sql){
   $b=mysql_query($sql,$this->conn);
   if(!$b){
    return 0;
   }else{
    if(mysql_affected_rows($this->conn)>0){
     return 1;//表示执行成功
    }else{
     return 2;//表示没有行数受到影响。
    }
   }
   
  }
  
  /*
   * 关闭连接,释放资源。
   */
  public function close_connect(){
   if(!empty($this->conn)){
    mysql_close($this->conn);
   }
  }
  
  
 }

?>

 

抱歉!评论已关闭.