+ -
当前位置:首页 → 问答吧 → PHP数据库操作类

PHP数据库操作类

时间:2008-10-22

来源:互联网

是PHPLIB的数据库操作类的扩展,用起来不错:

[code]<?php
/*
* 继承MyDd_MySql类.
*/
class MyDd_MySql extends DB_MYSQL {

   /******************************
    * 插入记录
    * input parameter
            : $table        => 表名
            : $field    => 参数数组:$array['字段名'] = 值.
    * out    parameter
            : ture or false
   ******************************/
   function InsertDB($table, $field){
      $Afield = $this->metadata($table);        
      $Sql = "INSERT INTO `$table`";
      foreach($field as $key=>$value){
         $fields .= "`$key`,";
         foreach($Afield as $fname){
            if($fname['name']==$key){
                $Ftyle = $fname['type'];
                break;
            }
         }
         if($Ftyle=="string"||$Ftyle=="blob"||$Ftyle=="date") $values.="'$value',";
         else $values.="$value,";
      }
      $fields = substr($fields, 0, -1);
      $values = substr($values, 0, -1);
      $Sql .= " (".$fields.")";
      $Sql .= " VALUES (".$values.")";
      //die($Sql);
      return $this->query($Sql);
   }
   
    /******************************
    * 删除记录
    * input parameter
            : $table        => 表名
            : $where        => 删除条件
    * out    parameter
            true or false
    ******************************/
    function DeleteDB($table, $where='1') {
        if(empty($table)) die("table is empty!");
        $deleteSql = "DELETE FROM `$table` WHERE $where";
        return $this->query($deleteSql);
    }
   
    /******************************
        * 更新记录
        * input parameter
                : $table        => 表名
                : $field    => 参数数组:$array['字段名'] = 值.
                : $where        => 更新条件
        * out    parameter
                : ture or false
    ******************************/
    function UpdateDB($table, $field, $where='1'){
      $Afield = $this->metadata($table);
      $Sql = "UPDATE `$table` SET";
      if(is_array($field)){
         foreach($field as $key => $var){
           foreach($Afield as $fname){
             if($fname['name']==$key){
               if($fname['type']=="string"||$fname['type']=="blob"||$fname['type']=="date") $Sql.=" `$key`='$var',";
                  else $Sql.=" `$key`=$var,";
                  break;
             }
           }
         }
         $Sql = substr($Sql, 0, -1);
      }else{
        $Sql .= trim($field);
      }
      $Sql .= ' WHERE '.$where;
      //die($Sql);
      return $this->query($Sql);
    }
   
    /******************************
        * 查询记录
        * input parameter
                : $table        => 表名
                : $field        => 表字段
                : $where        => 查询条件
                : $order        => 排序方式
                : $limit        => 显示数目
        * out    parameter
                : $array        => 信息数组(二维数组)
    ******************************/
    function SelectDB($table, $field='', $where='', $order='', $limit=''){
        $Sql = "select ";
        if(is_array($field)){
            foreach ($field as $key){
                $felids .= "`$key`,";
            }
            $felids = substr($felids, 0, -1);
        }else{
            $felids = "*";
        }
        $Sql .= $felids." from `".$table."` where 1";
        if($where != '') $Sql .= " and ".$where;
        if($order != '') $Sql .= " order by ".$order;
        if($limit != '') $Sql .= " limit ".$limit;
        //die($Sql);
        $result = $this->query($Sql);
        $array = array();
        while($row = $this->fetch_array($result)){
             $array[] = $row;
         }        
        return $array;
    }
        
  /******************************
        * 统计数据总数
        * input parameter  
                : $table            => 表名
        * out    parameter
                : $countArr['num']    => 返回条件数据总数
    ******************************/
   function CountData($table, $where='') {
       $countSql = "SELECT count(*) as num FROM $table";
       $countSql .= ($where == '') ? '' : " WHERE $where";
       $countResult = $this->query($countSql);
       $countArr = $this->fetch_array($countResult);
       //die($countArr['num']);
       return $countArr['num'];
   }
  
  /******************************
        * 分页
        * input parameter  
                : $pageNum            => 每页显示记录数
                : $table            => 表名
                : $where            => 查询条件
        * out    parameter
                : $limit            => 返回用于第二次查询的SQL中的limit部分
    ******************************/
  function LimitStr($table, $pageNum=10, $where='', $str='str', $view=true, $jump=true){
    global $tpl;
    //当前页码、每页显示数、开始记录
    $page = is_numeric($_GET['page']) ? $_GET['page'] : 1;
    if($page<1) $page = 1;
    $start = ($page-1)*$pageNum;
    $limit = "$start, $pageNum";
    //记录总数
    $total = $this->CountData($table, $where);
    //分页字符串
    $PageObj = new PAGE($total, $pageNum);
    $pagestr = $PageObj->StartPage($str, $view, $jump);
    $tpl->set_var("pagestr", $pagestr); //替换模板分页变量
    //返回limt:$start,$pageNum
    return $limit;
  }
  
  /*某一条记录*/
  function RecordOne($table, $where='1', $field='*'){
    $sql = 'select '.$field.' from '.$table.' where '.$where;
    $this->query($sql);
      $this->next_record();
    return $this->Record;
  }
  /*获取最大ID号*/
  function MaxID($table, $field='id'){
    $sql = 'select MAX('.$field.') as MaxId from '.$table;
    $this->query($sql);
      $this->next_record();     
    return $this->Record['MaxId'];
  }
  /*记录集数组*/
  function fetch_array($result){
    return mysql_fetch_array($result, MYSQL_ASSOC);
  }
  /*设置数据库字符集*/
  function SetNames($str){
     mysql_query("set names '$str'", $this->Link_ID);
  }
  /*输出SQL语句,调试之用*/
  function SQL($str){
     die($str);
  }
  
  /*JS提示框*/
  function forward($msg, $methd='', $url = ''){
     $sStr = "<script language='javascript' type='text/javascript'>\n";
     if($methd = 'href' && $url = '') die('forward funciton is wrong!');
     $sStr .= "    alert('$msg!'); \n";
     switch ($methd){
        case "href":
           $sStr .= " location.href='".$url."'; \n";
           break;
        case "close":
           $sStr .= " self.close(); \n";
           break;
        default:
           $sStr .= " history.go(-1); \n";
     }
     $sStr .= "</script>";
     die($sStr);
}

}//class End
?>[/code]

作者: tonlywang   发布时间: 2008-10-22

我还是比较喜欢使用把SQL写在外面的,改起来方便.

作者: gincn   发布时间: 2008-10-22

我发现:对于我来说,写比看容易多了……
我觉得与其光把代码写在这里,不如把大概思路简述下,然后再公布代码,那样说不定别人根据你的思路,会写出更漂亮、更适合自己使用的,呵呵

作者: shanhun59   发布时间: 2008-10-22

SQL写在外面所能改的,经过此扩展类后,依然可以改

作者: kebiaowang   发布时间: 2008-10-22

回复3#

        绝对是原创,你怎么说是光盘里的呢?

作者: tonlywang   发布时间: 2008-10-22

看一下,路过

作者: cnITonline.com   发布时间: 2008-10-25