PHP 数据库操作类

 <?php
/********************************************
FileName:statistic.class.php
Function:Statistic
Date:      2003-10-12 20:35
Author:      Kelphen
CopyRight:Capinfo Co.,ltd.
Modify:
********************************************/
class DB_MYSQL {
  var $Host     = "";//数据库连接服务器
  var $Database = "";//数据库名称
  var $User     = "";//数据库连接用户
  var $Password = "";//数据库连接密码

  var $Link_ID  = 0;//连接数据库返回指针
  var $Query_ID = 0;//数据库记录集指针
  var $Record   = array();//记录集返回数组
  var $Row      = 0;//

  var $Errno    = 0;//错误编号
  var $Error    = "";//错误内容

  var $Auto_Free = 1;     //数据库自动释放
  var $PConnect  = 0;     //持久连接数据库

  var $Page_Size=10;    //每页显示的记录行数
  var $Page=0;    //当前页数;
  var $MaxPage=0;    //记录总页数
  var $SumRows=0;    //记录集总行数

  var $arrSql; //预执行查询数组
  var $arrValue; //预执行查询值

  function DB_MYSQL($Host,$Database,$User,$Password,$query = "") {
/*
    功能:构造函件
*/
        $this->Host=$Host;
        $this->Database=$Database;
        $this->User=$User;
        $this->Password=$Password;


  }

  function connect() {
/*
    功能:连接数据库
*/
    if ( 0 == $this->Link_ID ) {
      if(!$this->PConnect) {
        $this->Link_ID = mysql_connect($this->Host, $this->User, $this->Password);
      } else {
        $this->Link_ID = mysql_pconnect($this->Host, $this->User, $this->Password);
      }
      if (!$this->Link_ID)
        $this->halt("数据库错误","connect($this->Host, $this->User, /$Password) failed.");
      else
          mysql_select_db($this->Database, $this->Link_ID);
    }
  }
  function free_result(){
/*
    功能:释放目前SQL SERVER数据库query函数执行所占用的内存
*/
    mysql_free_result($this->Query_ID);
      $this->Query_ID = 0;
  }

  function query($Query_String){
/*
    功能:执行SQL语句
*/
    if ($Query_String == "")
        return 0;
      if (!$this->Link_ID)
        $this->connect();
    $this->Query_ID = mysql_query($Query_String, $this->Link_ID);
    $this->Row = 0;
    //$this->Row = $this->seek($pos);
    if (!$this->Query_ID) {
      $this->Errno = 1;
      $this->Error = "General Error (SQL SERVER 数据库不能返回详细的错误信息).";
      $this->halt("数据库错误","无效的 SQL 语句: ".$Query_String);
    }
    return $this->Query_ID;
  }
  function verQuery($Query_String){
/*
    功能:验证SQL语句是否正确
*/
    if ($Query_String == "")
        return 0;
      if (!$this->Link_ID)
        $this->connect();
    $this->Query_ID = mysql_query($Query_String, $this->Link_ID);
    $this->Row = 0;
    //$this->Row = $this->seek($pos);
    if (!$this->Query_ID) {
      return 0;
    }
    return 1;
  }

  function next_record() {
/*
    功能:记录集指针下移并且读取当前记录值
*/
    if ($this->Record = mysql_fetch_row($this->Query_ID)) {
      // add to Record[<key>]
      $count = mysql_num_fields($this->Query_ID);
      for ($i=0; $i<$count; $i++){
          $fieldinfo = mysql_fetch_field($this->Query_ID,$i);
        $this->Record[strtolower($fieldinfo->name)] = $this->Record[$i];
      }
      $this->Row += 1;
      $stat = 1;
    } else {
      if ($this->Auto_Free) {
            $this->free_result();
          }
      $stat = 0;
    }
    return $stat;
  }

/*******************************
    功能:移动指针到指定行
*******************************/
    function seek($pos){
        mysql_data_seek($this->Query_ID,$pos);
        if($pos!=""){
            $this->Row = $pos;//记录指针
        }else{
            $this->Row=0;
        }
    }

  function metadata($table){
/*
    功能:返回指定表格的字段信息
*/
    $count = 0;
    $id    = 0;
    $res   = array();

    $this->connect();
    $id = mysql_query("select * from $table", $this->Link_ID);
    if (!$id) {
      $this->Errno = 1;
      $this->Error = "General Error (SQL SERVER 数据库不能返回详细的错误信息).";
      $this->halt("数据库错误","Metadata query 失败.");
    }
    $count = mysql_num_fields($id);

    for ($i=0; $i<$count; $i++) {
        $info = mysql_fetch_field($id, $i);
      $res[$i]["table"] = $table;
      $res[$i]["name"]  = $info->name;
      $res[$i]["len"]   = $info->max_length;
      $res[$i]["flags"] = $info->numeric;
    }
    //$this->free_result();
    return $res;
  }


  //以下是预执行程序
  /**************************************/
   function prepareExecute($sql){
        $this->arrSql = explode("?",$sql);
        $this->arrValue = null;
   }

   function setInt($position,$value){
           $this->arrValue[$position-1] = $value;
   }
   function setString($position,$value){
           $this->arrValue[$position-1] = "'".$value."'";
   }

   function executeUpdate($show_sql = false){
           $sql = "";
      for($i=0;$i<sizeof($this->arrSql);$i++){
          //echo $this->arrSql[$i]."==".$this->arrValue[$i]."<br>";
          $sql.= $this->arrSql[$i].$this->arrValue[$i];
      }
      if($show_sql)
          echo $sql;

      $this->query($sql);
   }

   /*********************************************/

   function last_id() {
/*
    功能:获得插入的最后一个ID号
*/
    $rsRows = mysql_query("Select @@IDENTITY as rows", $this->Link_ID);
    if ($rsRows) {
       return mysql_result($rsRows, 0, "rows");
    }
  }

  function close(){
       mysql_close();
  }
  function num_rows() {
/*
    功能:获得所有记录数目
*/
    return mysql_num_rows($this->Query_ID);
  }

  function num_fields() {
/*
    功能:获得字段数目
*/
    return mysql_num_fields($this->Query_ID);
  }

  function f($Field_Name)
/*
    功能:得到之前插入的ID号
*/
  {
    foreach ($this->Record as $key => $value)
    {
        if($key==strtolower($Field_Name))
      {
          return $this->Record[strtolower($Field_Name)];
      }
    }
    $this->Errno = 3;
    $this->Error = "General Error (SQL SERVER 数据库不能返回详细的错误信息).";
    $this->halt("数据库错误","列名".$Field_Name."无效");
  }

  function p($Field_Name) {
/*
    功能:输出字段内容
*/
    print $this->f($Field_Name);
  }

  function Set_Error($error_no,$error_title) {
/*
    功能:设置错误信息
*/
    $this->Errno=$error_no;
    $this->Error=$error_title;
  }

  function halt($type,$msg) {
/*
    功能:系统停止并且抛出一个错误
*/
    printf("<b>$type:</b> %s<br>/n", $msg);
    printf("<b>系统提示错误</b>: %s (%s)<br>/n",$this->Errno,$this->Error);
    die("操作异常停止.");
  }
  function set_Auto_Free($flag){
/*
    功能:设置Auto_Free属性值
*/
      $this->Auto_Free=$flag;
  }
  function set_PConnect($flag){
/*
    功能:设置PConnect属性值
*/
      $this->PConnect=$flag;
  }

/**********************************************************
    功能:替除page=页数链接
**********************************************************/
    function query_cut_str($cut_str){
        if(sizeof($_POST)>0){
            foreach ($_POST as $key => $value) {
                //循环分析出$_POST变量的键值
                if (!empty($value)){
                    $str .=$key."=".urlencode($value)."&";
                }
            }
        }
        return ereg_replace($cut_str."[0-9]+&*","",$GLOBALS["QUERY_STRING"].$str);
    }


/**
 * 初始化分页参数,对数据集按照页的大小进行分块,做分页的准备
 *
 * @param        $page_size    每页的记录数量
 * @param        $page        当前页,一般不用传递实参
 * @access        private
 * @return        boolean
 */
    function setRecorderPointer($page_size,$page){
        $this->Page_Size=$page_size;
        $this->Page=$page;
        //记录总数量
        $this->SumRows=$this->num_rows();
        if($this->SumRows!=0){//如果记录总数不为0
            if($this->Page==0 or is_null($this->Page) or (!is_numeric($this->Page))){//所有非法页数都被设置为1
                $this->Page=1;
            }
            //获得记录总页数
            $this->MaxPage = (int)ceil($this->SumRows/$this->Page_Size);
            if((int)$this->Page > $this->MaxPage){//如果当前页大于总页数
                $this->Page=$this->MaxPage;
            }
            //移动记录集指针
            $this->seek(($this->Page-1)*$this->Page_Size);
        }else{//记录总数为0,返回false
            $this->Page=0;
            $this->MaxPage=0;
            return 0;
        }
    }
    /**
 * 分页导航,直接调用此方法,对分页的设置进行隐藏.
 *
 * @access public
 * @void sting
 */
    function getPageNavigation(){
        //初始化分页参数,做分页的准备
        $PrevPage=0;
        $NextPage=0;
        $StrPage="";
        //首页和上一页的链接
        if( $this->SumRows>1 && $this->Page>1){
            $PrevPage=$this->Page-1;
            $StrPage.="<font size=2> ";
            $StrPage.=" <a href=$PHP_SELF?page=1&".$this->dealPagePara("page=").">首页</a> ";
            $StrPage.=" <a href=$PHP_SELF?page=$PrevPage&".$this->dealPagePara("page=")." >上一页</a> ";
            $StrPage.="</font>";
        }else{
            $StrPage.="<font color=#c0c0c0 size=2>首页</font> ";
            $StrPage.="<font color=#c0c0c0 size=2>上一页</font> ";
        }
        //下一页和末页的链接
        if( $this->Page>=1 && $this->Page<$this->MaxPage){
            $NextPage= $this->Page+1;
            $StrPage.="<font size=2> ";
            $StrPage.=" <a href=$PHP_SELF?page=$NextPage&".$this->dealPagePara("page=")." >下一页</a> ";
            $StrPage.=" <a href=$PHP_SELF?page=$this->MaxPage&".$this->dealPagePara("page=")." >末页</a> ";
            $StrPage.="</font>";
        }else{
            $StrPage.=" <font color=#c0c0c0 size=2>下一页</font> ";
            $StrPage.=" <font color=#c0c0c0 size=2>末页</font> ";
        }
        $StrPage.=" <font size=2>第 $this->Page 页,共 $this->MaxPage 页,共有 $this->SumRows 条记录</font>";
        return $StrPage;
    }
/**
 * 处理分页条件参数,包括GET和POST
 *
 * @param $para_get 一般是page字符串
 * @access private
 * @void string
 */
function dealPagePara($para_get){
        if(sizeof($_POST)>0){
            foreach ($_POST as $key => $value) {
                //循环分析出$_POST变量的键值
                if (!empty($value)){
                    $para_post.=$key."=".urlencode($value)."&";
                }
            }
        }
        return ereg_replace($para_get."[0-9]+&*","",$GLOBALS["QUERY_STRING"].$para_post);
    }

  function Set_Page_Start($page_size,$page){
/*
    功能:分页的开始
*/
      $this->Set_Page_Size($page_size);
    $this->Set_Page($page);
    $this->SumRows=$this->num_rows();//记录总数量
    if($this->SumRows!=0)
    {
        if($this->Page==0 or is_null($this->Page) or (!is_numeric($this->Page)))//所有非法页数都被设置为1
        {
            $this->Page=1;
        }
        $this->MaxPage = (int)ceil($this->SumRows/$this->Page_Size);//获得记录总页数
        if((int)$this->Page > $this->MaxPage)
        {
            $this->Page=$this->MaxPage;
        }
        $this->seek(($this->Page-1)*$this->Page_Size);
    }
    else
    {
        $this->Page=0;
        $this->MaxPage=0;
        return 0;
    }
  }

  function Set_Page_Size($page_size){
/*
    功能:设置每页显示的记录数量
*/
      $this->Page_Size=$page_size;
  }

  function Get_Page_Size(){
/*
    功能:返回每页显示的记录数量
*/
      return $this->Page_Size;
  }
function SumRecord()
/*
    功能:返回记录集的总条数
*/
{
    return mysql_num_rows($this->Query_ID);
}
  function Set_Page($page){
/*
    功能:设置当前页数
*/
    $this->Page=$page;
  }

  function page_nav(){
/*
    功能:分页导航
*/
    if(is_null($this->Page) or is_null($this->MaxPage) or is_null($this->SumRows))
    {
        $this->Set_Error(21,"请您先调用Set_Page_Start()方法显示文章列表!");
        $this->halt("封装类内部错误",'函数page_nav()操作无效');
    }
    $PrevPage=0;
    $NextPage=0;
    $StrPage="";
    //首页和上一页的链接
    if( $this->SumRows>1 && $this->Page>1)
    {
        $PrevPage=$this->Page-1;
        $StrPage.=" <a href=$PHP_SELF?page=1&".$this->query_cut_str("page=").">首页</a> ";
        $StrPage.=" <a href=$PHP_SELF?page=$PrevPage&".$this->query_cut_str("page=")." >上一页</a> ";
     }
     else
     {
        $StrPage.="<font color=#c0c0c0>首页</font> ";
        $StrPage.="<font color=#c0c0c0>上一页</font> ";
     }
     //下一页和末页的链接
     if( $this->Page>=1 && $this->Page<$this->MaxPage)
     {
        $NextPage= $this->Page+1;
        $StrPage.=" <a href=$PHP_SELF?page=$NextPage&".$this->query_cut_str("page=")." >下一页</a> ";
        $StrPage.=" <a href=$PHP_SELF?page=$this->MaxPage&".$this->query_cut_str("page=")." >末页</a> ";
      }
     else
     {
        $StrPage.=" <font color=#c0c0c0>下一页</font> ";
        $StrPage.=" <font color=#c0c0c0>末页</font> ";
      }
      $StrPage.=" <font size=2>第 $this->Page 页,共 $this->MaxPage 页,共有 $this->SumRows 条记录</font>";
      return $StrPage;
}
  function page_nav1($type=1,$class=""){
/*
    功能:分页导航
*/
    if(is_null($this->Page) or is_null($this->MaxPage) or is_null($this->SumRows))
    {
        $this->Set_Error(21,"请您先调用Set_Page_Start()方法显示文章列表!");
        $this->halt("封装类内部错误",'函数page_nav()操作无效');
    }
    $PrevPage=0;
    $NextPage=0;
    $StrPage="";
    //首页和上一页的链接
    if( $this->SumRows>1 && $this->Page>1)
    {
        $PrevPage=$this->Page-1;
        $StrPage.=" <a href=$PHP_SELF?page=1&".$this->query_cut_str("page=")." ".($class?"class='$class'":"").">首页</a> ";
        $StrPage.=" <a href=$PHP_SELF?page=$PrevPage&".$this->query_cut_str("page=")." ".($class?"class='$class'":"").">上一页</a> ";
     }
     else
     {
        $StrPage.="<font color=#c0c0c0>首页</font> ";
        $StrPage.="<font color=#c0c0c0>上一页</font> ";
     }
     //下一页和末页的链接
     if( $this->Page>=1 && $this->Page<$this->MaxPage)
     {
        $NextPage= $this->Page+1;
        $StrPage.=" <a href=$PHP_SELF?page=$NextPage&".$this->query_cut_str("page=")." ".($class?"class='$class'":"").">下一页</a> ";
        $StrPage.=" <a href=$PHP_SELF?page=$this->MaxPage&".$this->query_cut_str("page=")." ".($class?"class='$class'":"").">末页</a> ";
      }
     else
     {
        $StrPage.=" <font color=#c0c0c0>下一页</font> ";
        $StrPage.=" <font color=#c0c0c0>末页</font> ";
      }
    //  $StrPage.="ON $this->Page PAGE,All $this->MaxPage PAGE,ALL $this->SumRows RECORD&nbsp;";
      $StrPage.=" <font size=2>第 $this->Page 页,共 $this->MaxPage 页,共有 $this->SumRows 条记录</font>";
      if($type==1){
            $str_script="/n<script language='javascript'>/n function checknav(form){/n if(isNaN(form.nav_text.value) || form.nav_text.value.length==0 || form.nav_text.value.indexOf(/"-/")!=-1 || form.nav_text.value!=parseInt(form.nav_text.value).toString()){ /n alert('请填写正整数型页码!'); /n return false; /n }else{ /n  window.location.href='".$PHP_SELF."?page='+form.nav_text.value+'&".$this->query_cut_str("page=")."';  /n} /n } /n </script> /n";
            $StrPage=$str_script."<form name='form_nav' action=''>".$StrPage."<input name='nav_text' type='text'  size='5' style='font-size: 12px; border: #666666; border-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px'>&nbsp;<input type='button' name='Submit' value='go' style='font-size: 12px; border: #666666; border-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px' οnclick='checknav(this.form)'>"."</form>";
      }else if($type==2){
          $str_script="/n<script language='javascript'>/n function checknav(form){/n if(isNaN(form.nav_text.value) || form.nav_text.value.length==0 || form.nav_text.value.indexOf(/"-/")!=-1 || form.nav_text.value!=parseInt(form.nav_text.value).toString()){ /n alert('请填写正整数型页码!'); /n return false; /n }else{ /n  window.location.href='".$PHP_SELF."?page='+form.nav_text.value+'&".$this->query_cut_str("page=")."';  /n} /n } /n </script> /n";
          $StrPage=$str_script."<form name='form_nav' action=''>".$StrPage."<input type='hidden' name='nav_text'><select style='font-size: 12px; border: #666666; border-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px' οnchange='this.form.nav_text.value=this.value;checknav(this.form)'>";
          for($i=1;$i<=$this->MaxPage;$i++){
            $StrPage.="<option value=$i ".($this->Page==$i?"selected":"").">".$i."</option>";
          }
          $StrPage.="</select></form>";
      }
      return $StrPage;
}

}
?>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值