国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

MYSQL的操作類(修改后的新版本)

2024-07-24 12:54:48
字體:
來源:轉載
供稿:網友

 

 

 

 


  class mysqldb
  {
    //mysql數據庫操作類
    //作者:熊毅
    //版本:2.0(發行版)
   
    //可以自由轉載,修改請通知我[email protected]
    //轉載請保留以上聲明
   
    //使用說明:
    //該類完全按照ado的習慣書寫的,用過asp的人都覺得asp連接數據庫比php好用(這是我的感覺),
    //但php得一個一個api地寫,挺累,該類做了完全的封裝
    //創建類的實例時可以指定一個數據庫表和選擇的數據庫,如:new mysqldb("table","database");
    //查詢數據時query后可以用getvalue得到相應的值,既可以是字段名也可以是已0開始的序號
    //插入新值,先用addnew后使用setvalue相應的字段名或序號和字段值,在用update添加
    //編輯時用edit指定編輯記錄的條件在使用setvalue,最后用update添加
    //在類使用過程中,stname記錄上次使用的數據庫表名,當指定后可以直接使用,以后的操作默認在該表
    //上進行操作,當然也可以每次指定特殊的表進行操作
    //nerr指示是否操作出錯,serr記錄最后一次出錯的錯誤代碼,記錄了明確的有哪個函數引起的錯誤
    //錯誤之處請指正
    //歡迎來信與我交流編程經驗:[email protected]
    //我的csdn:用戶號:scxy;呢稱:小熊,請多關照
   
    //可以自由轉載,修改請通知我[email protected]
    //轉載請保留以上聲明

    var $host="localhost";        //主機名
    var $user="boot";           //用戶名
    var $password="oaserver";   //用戶密碼
    var $linkid;                 //連接值
    var $dbid;                   //數據庫選擇的結果值
    var $stname;                  //指定當前操作的數據庫表
    var $serr;                   //錯誤代碼
    var $nerr;                   //指示是否有錯誤存在,0無錯誤,1有錯誤
    var $nresult;                //查詢結果值
    var $afname;                 //保存fieldsname的數組
    var $nrows;                  //查詢結果中的行數
    var $ncols;                  //查詢結果中的列數
    var $anew;                   //添加在addnew函數后的數據,以數組形式保存
    var $newedit;                  //判斷當前是否在進行添加操作,0表示沒有,1表示在進行添加,2表示編輯
    var $seditcon;               //指定編輯記錄的條件
    var $noffset;                //記錄偏移量
    var $eof;                     //標記是否到記錄集尾
    var $ssql;                    //最后一條執行的sql語句

    //執行update所要用到的全局變量
    var $sname;                   //字段名
    var $svalue;                  //字段值addnew時用
    var $sedit;                   //字段值edit時用

    function initialize()
    {
      $this->nerr=0;
      $this->newedit=0;
      $this->nresult=-1;
      $this->ncols=0;
      $this->nrows=0;
      $this->noffset=0;
      $this->eof=true;
      $this->sname="";
      $this->svalue="#@!";
      $this->sedit="#@!";
      unset($this->afname);
      unset($this->anew);
    }
    function mysqldb($tablename="",$database="slt")  //構造函數
    {
      $this->initialize();
      $this->stname=$tablename;
      $this->linkid=mysql_connect($host,$user,$password);
      if(!$this->linkid)
      {
        $this->nerr=1;
        $this->serr="mysqldb:數據庫連接出錯,請啟動服務!";
        return;
      }
      $this->dbid=mysql_select_db($database);
      if(!$this->dbid)
      {
        $this->nerr=1;
        $this->serr="mysqldb:選擇的數據庫".$database."不存在!";
        return;
      }
    }

    function isempty($value)
    {
            if(is_string($value)&&empty($value))
               return true;
            return false;
    }

    function destroy()          //數據清除處理
    {
      mysql_query("commit");
      mysql_close();
    }

    function printerr()
    {
      if($this->nerr==1)
      {
        echo($this->serr."<br><br>");
      }
      else
      {
        echo("沒有錯誤<br><br>");
      }
    }

        function execute($sql)  //直接執行sql語句
          {
                if(empty($sql))
                  {
                        $this->nerr=1;
                        $this->serr="execute:執行語句不能為空!";
                        return false;
                  }
                 $this->ssql=$sql;
                  if(!mysql_query($sql))
                  {
                          $this->nerr=1;
                          $this->serr="execute:sql語句:".$sql."<br>mysql錯誤:".mysql_error();
                          return false;
                  }
                  return true;
          }

    function query($tablename="",$sql="*",$condition="",$order="",$sequenc="") //在數據庫里執行查詢
    {
      $this->initialize();
      if(!empty($tablename))
        $this->stname=$tablename;
      $strsql="select ".$sql." from ".$this->stname;
      if(!empty($condition))
        $strsql=$strsql." where ".$condition;
      if(!empty($order))
        $strsql=$strsql." order by ".$order;
      if(!empty($sequenc))
        $strsql=$strsql." ".$sequenc;
          $this->ssql=$strsql;
      if(!$this->nresult=mysql_query($strsql))
      {
        $this->nerr=1;
        $this->serr="query:sql語句:".$strsql."<br>mysql錯誤:".mysql_error()."<br>";
        return;
      }
      $this->noffset=0;
      $this->nrows=mysql_num_rows($this->nresult);
      $this->ncols=mysql_num_fields($this->nresult);
          if($this->nrows>0)
                  $this->eof=false;
          else
                  $this->eof=true;
      unset($this->afname);
      $this->afname=array();
      for($i=0;$i<$this->ncols;$i++)
         $this->afname[$i]=strtolower(mysql_field_name($this->nresult,$i));
    }

        function movenext()
          {
                if($this->eof)
                  {
                        $this->nerr=1;
                        $this->serr="movenext:已經移到記錄集末尾!";
                        return;
                  }
                $this->noffset++;
                if($this->noffset>=$this->nrows)
                        $this->eof=true;
          }

     function moveto($offset)
     {
        if(empty($offset))
        {
          $this->nerr=1;
          $this->serr="moveto:必須指定偏移量! ";
          return;
        }

        if(!$this->nresult)
        {
          $this->nerr=1;
          $this->serr="moveto:請先執行查詢:query";
          return;
        }
        $this->noffset=$offset;
     }

    //得到指定行的指定列的值,返回字符串
    //如果不指定offset將取得下一行的值
    //如果不指定nfields將取得該行的值,并已數組形式返回
    function getvalue($nfields=-1,$offset=-1)
    {
      if($this->nresult==-1)
      {
        $this->nerr=1;
        $this->serr="getvalue:請先執行query()函數!";
        return;
      }
      if($offset>-1)
      {
                $this->noffset=$offset;
        if($this->noffset>=$this->nrows)
        {
          $this->nerr=1;
          $this->serr="getvalue:所要求的偏移量太大,無法達到!";
          return;
        }
      }
           if([email protected]_data_seek($this->nresult,$this->noffset))
                {
                  $this->nerr=1;
                  $this->serr="getvalue:請求不存在的記錄!";
                  return;
                }
      $aresult=mysql_fetch_row($this->nresult);
      if(is_int($nfields)&&$nfields>-1)
      {
        if($nfileds>$this->ncols)
        {
          $this->nerr=1;
          $this->serr="getvalue:所請求的列值大于實際的列值!";
          return;
        }
        return $aresult[$nfields];
      }
          if(is_string($nfields))
          {
                $nfields=strtolower($nfields);
            for($i=0;$i<$this->ncols;$i++)
                {
                  if($this->afname[$i]==$nfields)
                          break;
                }
                if($i==$this->ncols)
                  {
                        $this->nerr=1;
                        $this->serr="getvalue:所請求的列不存在,請仔細檢查!";
                        return;
                  }
                  return $aresult[$i];
          }
      return $aresult;
    }

    function addnew($tablename="")  //標志開始添加數據
    {
      $this->initialize();
      if(!empty($tablename))
        $this->stname=$tablename;
      if($this->newedit>0)
      {
        $this->nerr=1;
        $this->serr="addnew:你正在對數據庫進行添加或更新操作!";
        return;
      }
      if(empty($this->stname))
      {
        $this->nerr=1;
        $this->serr="addnew:想要添加的數據庫表為空,可以在構造時指定,也可在addnew()時指定!";
        return;
      }
      unset($this->anew);
      $this->anew=array();
      $this->newedit=1;
      $strsql="select * from ".$this->stname;
          $this->ssql=$strsql;
      if(!$this->nresult=mysql_query($strsql))
      {
        $this->nerr=1;
        $this->serr="addnew:sql語句:".strsql."<br><br>mysql錯誤:".mysql_error();
        return;
      }
      $this->ncols=mysql_num_fields($this->nresult);
      unset($this->afname);
      $this->afname=array();
      for($i=0;$i<$this->ncols;$i++)
         $this->afname[$i]=strtolower(mysql_field_name($this->nresult,$i));
    }

    function edit($condition="",$tablename="")  //對指定數據庫表進行編輯
    {
                  $this->initialize();
                  if(!empty($tablename))
                          $this->stname=$tablename;
                  $this->seditcon=$condition;
                  if(empty($this->stname))
                  {
                          $this->nerr=1;
                          $this->serr="edit:在編輯前請先指定數據庫表!";
                          return;
                  }
                  unset($this->anew);
                  $this->anew=array();
                  $this->newedit=2;
                  $strsql="select * from ".$this->stname;
                  $this->ssql=$strsql;
                  if(!$this->nresult=mysql_query($strsql))
          {
             $this->nerr=1;
             $this->serr="edit:sql語句:".strsql."<br><br>mysql錯誤:".mysql_error();
             return;
          }
          $this->ncols=mysql_num_fields($this->nresult);
          unset($this->afname);
          $this->afname=array();
          for($i=0;$i<$this->ncols;$i++)
             $this->afname[$i]=strtolower(mysql_field_name($this->nresult,$i));
    }

    function setvalue($index,$value) //指定數據,跟在addnew后執行;
    {
             if($this->newedit==0)
             {
                $this->nerr=1;
                $this->serr="setvalue:請先執行addnew()或者edit()!";
                return;
             }
             if(is_int($index))
             {
                 if($index<0||$index>$this->ncols)
                 {
                    $this->nerr=1;
                    $this->serr="setvalue:插入不存在的列值!";
                    return;
                 }
                 $this->anew[$index]=$value;
                 $tmpin=$index;
             }
             elseif(is_string($index))
             {
                $index=strtolower($index);
                for($i=0;$i<$this->ncols;$i++)
                {
                    if($this->afname[$i]==$index)
                       break;
                }
                if($i==$this->ncols)
                {
                    $this->nerr=1;
                    $this->serr="setvalue:插入不存在的列值!";
                    return;
                 }
                 $this->anew[$i]=$value;
                 $tmpin=$i;
             }
                 if(!empty($this->sname))
                    $this->sname.=",";
                 $this->sname.=$this->afname[$tmpin];
                 //根據當前字段的類型生成相應的新值
                 if($this->svalue!="#@!")
                    $this->svalue.=",";
                 else
                    $this->svalue="";
                 $[email protected]mysql_field_type($this->nresult,$i);
                 //echo($ftype.",".$this->anew[$i].",".$i.":".$svalue."<br>");
                
                 switch($ftype)
                 {
                  case "string":
                  case "date":
                  case "datetime":
                        $this->svalue.="/"".$this->anew[$tmpin]."/"";
                        $this->sedit="/"".$this->anew[$tmpin]."/"";
                        break;
                  case "int":
                  case "unknown":
                       $this->svalue.=$this->anew[$tmpin];
                       $this->sedit=$this->anew[$tmpin];
                       break;
                  default:
                       $this->nerr=1;
                       $this->serr="update:字段名為".$this->afname[$tmpin]."的".$ftype."類型目前版本不支持,請用別的方法添加數據!";
                       return;
                 }

                 if($this->newedit==2)
                    $this->sname.="=".$this->sedit;
      }

    function update()    //存儲新值到數據庫
    {
      $strsql="";

      if($this->newedit==0)
      {
        $this->nerr=1;
        $this->serr="update:請先執行addnew()或者edit(),再用setvalue()添加值!";
        return;
      }

      if(empty($this->svalue))
      {
        $this->nerr=1;
        $this->serr="update:在數據為空的情況下,不能添加或修改數據!";
        return;
      }

      switch($this->newedit)
      {
       case 1:       //添加
            $strsql="insert into ";
            $strsql.=$this->stname;
            $strsql.=" (".$this->sname.") ";
            $strsql.="values (".$this->svalue.")";
            break;
      case 2:          //修改
            $strsql="update ";
            $strsql.=$this->stname;
            $strsql.=" set ";
            $strsql.=$this->sname;
            if(!empty($this->seditcon))
                $strsql.=" where ".$this->seditcon;
            break;
      default:
           $this->nerr=1;
           $this->serr="update:update()生成sql語句出錯,請檢查!";
           return;
      }

      $this->ssql=$strsql;
      if(!$this->nresult=mysql_query($strsql))
      {
        $this->nerr=1;
        $this->serr="update:sql語句:".$strsql."<br><br>mysql錯誤:".mysql_error();
        return;
      }
       //echo($this->ssql."<br>");
      //作清理工作
      $this->newedit=0;
      unset($this->anew);
      mysql_query("commit");
    }
  }
?>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 磐石市| 云南省| 永靖县| 广安市| 吉木乃县| 岱山县| 安多县| 寻乌县| 永寿县| 十堰市| 棋牌| 和林格尔县| 马关县| 绥滨县| 鄂伦春自治旗| 衡水市| 巴林右旗| 达孜县| 会理县| 廉江市| 江都市| 南开区| 邵阳县| 克什克腾旗| 哈密市| 金昌市| 尼玛县| 青海省| 虞城县| 江口县| 临沧市| 湖北省| 盖州市| 墨竹工卡县| 乌兰浩特市| 青龙| 县级市| 阳西县| 洛阳市| 汉中市| 石阡县|