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

首頁(yè) > 數(shù)據(jù)庫(kù) > MySQL > 正文

mysql仿asp的數(shù)據(jù)庫(kù)操作類

2020-01-19 00:24:33
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

具體代碼如下所示:

<?php class MySQLDB  {   //MYSQL數(shù)據(jù)庫(kù)操作類   //作者:熊毅   //版本:2.0(發(fā)行版)   //可以自由轉(zhuǎn)載,修改請(qǐng)通知我scxy78@yeah.net   //轉(zhuǎn)載請(qǐng)保留以上聲明     //上進(jìn)行操作,當(dāng)然也可以每次指定特殊的表進(jìn)行操作   //nErr指示是否操作出錯(cuò),sErr記錄最后一次出錯(cuò)的錯(cuò)誤代碼,記錄了明確的有哪個(gè)函數(shù)引起的錯(cuò)誤   //錯(cuò)誤之處請(qǐng)指正   //歡迎來(lái)信與我交流編程經(jīng)驗(yàn):scxy78@yeah.net   //我的CSDN:用戶號(hào):scxy;呢稱:小熊,請(qǐng)多關(guān)照   //可以自由轉(zhuǎn)載,修改請(qǐng)通知我scxy78@yeah.net   //轉(zhuǎn)載請(qǐng)保留以上聲明   var $host="localhost";    //主機(jī)名   var $user="boot";      //用戶名   var $password="oaserver";  //用戶密碼   var $linkid;         //連接值   var $dbid;          //數(shù)據(jù)庫(kù)選擇的結(jié)果值   var $sTName;         //指定當(dāng)前操作的數(shù)據(jù)庫(kù)表   var $sErr;          //錯(cuò)誤代碼   var $nErr;          //指示是否有錯(cuò)誤存在,0無(wú)錯(cuò)誤,1有錯(cuò)誤   var $nResult;        //查詢結(jié)果值   var $aFName;         //保存FieldsName的數(shù)組   var $nRows;         //查詢結(jié)果中的行數(shù)   var $nCols;         //查詢結(jié)果中的列數(shù)   var $aNew;          //添加在AddNew函數(shù)后的數(shù)據(jù),以數(shù)組形式保存   var $NewEdit;         //判斷當(dāng)前是否在進(jìn)行添加操作,0表示沒(méi)有,1表示在進(jìn)行添加,2表示編輯   var $sEditCon;        //指定編輯記錄的條件   var $nOffset;        //記錄偏移量   var $EOF;           //標(biāo)記是否到記錄集尾   var $sSQL;          //最后一條執(zhí)行的SQL語(yǔ)句   //執(zhí)行Update所要用到的全局變量   var $sName;          //字段名   var $sValue;         //字段值A(chǔ)ddNew時(shí)用   var $sEdit;          //字段值Edit時(shí)用   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") //構(gòu)造函數(shù)   {    $this->Initialize();    $this->sTName=$TableName;    $this->linkid=mysql_connect($host,$user,$password);    if(!$this->linkid)    {     $this->nErr=1;     $this->sErr="MySqlDB:數(shù)據(jù)庫(kù)連接出錯(cuò),請(qǐng)啟動(dòng)服務(wù)!";     return;    }    $this->dbid=mysql_select_db($database);    if(!$this->dbid)    {     $this->nErr=1;     $this->sErr="MySqlDB:選擇的數(shù)據(jù)庫(kù)".$database."不存在!";     return;    }   }   function IsEmpty($Value)   {       if(is_string($Value)&&empty($Value))         return true;       return false;   }   function Destroy()     //數(shù)據(jù)清除處理   {    mysql_query("commit");    mysql_close();   }   function PrintErr()   {    if($this->nErr==1)    {     echo($this->sErr."<br><br>");    }    else    {     echo("沒(méi)有錯(cuò)誤<br><br>");    }   }     function Execute($SQL) //直接執(zhí)行SQL語(yǔ)句      {         if(empty($SQL))          {             $this->nErr=1;             $this->sErr="Execute:執(zhí)行語(yǔ)句不能為空!";             return false;          }          $this->sSQL=$SQL;          if(!mysql_query($SQL))          {              $this->nErr=1;              $this->sErr="Execute:SQL語(yǔ)句:".$SQL."<br>MySql錯(cuò)誤:".mysql_error();              return false;          }          return true;      }   function Query($TableName="",$SQL="*",$Condition="",$Order="",$Sequenc="") //在數(shù)據(jù)庫(kù)里執(zhí)行查詢   {    $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語(yǔ)句:".$strSQL."<br>MySql錯(cuò)誤:".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:已經(jīng)移到記錄集末尾!";             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:請(qǐng)先執(zhí)行查詢:Query";      return;     }     $this->nOffset=$Offset;    }   //得到指定行的指定列的值,返回字符串   //如果不指定Offset將取得下一行的值   //如果不指定nFields將取得該行的值,并已數(shù)組形式返回   function GetValue($nFields=-1,$Offset=-1)   {    if($this->nResult==-1)    {     $this->nErr=1;     $this->sErr="GetValue:請(qǐng)先執(zhí)行Query()函數(shù)!";     return;    }    if($Offset>-1)    {         $this->nOffset=$Offset;     if($this->nOffset>=$this->nRows)     {      $this->nErr=1;      $this->sErr="GetValue:所要求的偏移量太大,無(wú)法達(dá)到!";      return;     }    }       if(!@mysql_data_seek($this->nResult,$this->nOffset))         {          $this->nErr=1;          $this->sErr="GetValue:請(qǐng)求不存在的記錄!";          return;         }    $aResult=mysql_fetch_row($this->nResult);    if(is_int($nFields)&&$nFields>-1)    {     if($nFileds>$this->nCols)     {      $this->nErr=1;      $this->sErr="GetValue:所請(qǐng)求的列值大于實(shí)際的列值!";      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:所請(qǐng)求的列不存在,請(qǐng)仔細(xì)檢查!";             return;          }          return $aResult[$i];      }    return $aResult;   }   function AddNew($TableName="") //標(biāo)志開(kāi)始添加數(shù)據(jù)   {    $this->Initialize();    if(!empty($TableName))     $this->sTName=$TableName;    if($this->NewEdit>0)    {     $this->nErr=1;     $this->sErr="AddNew:你正在對(duì)數(shù)據(jù)庫(kù)進(jìn)行添加或更新操作!";     return;    }    if(empty($this->sTName))    {     $this->nErr=1;     $this->sErr="AddNew:想要添加的數(shù)據(jù)庫(kù)表為空,可以在構(gòu)造時(shí)指定,也可在AddNew()時(shí)指定!";     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語(yǔ)句:".strSQL."<br><br>MySql錯(cuò)誤:".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="") //對(duì)指定數(shù)據(jù)庫(kù)表進(jìn)行編輯   {          $this->Initialize();          if(!empty($TableName))              $this->sTName=$TableName;          $this->sEditCon=$Condition;          if(empty($this->sTName))          {              $this->nErr=1;              $this->sErr="Edit:在編輯前請(qǐng)先指定數(shù)據(jù)庫(kù)表!";              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語(yǔ)句:".strSQL."<br><br>MySql錯(cuò)誤:".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) //指定數(shù)據(jù),跟在AddNew后執(zhí)行;   {        if($this->NewEdit==0)        {         $this->nErr=1;         $this->sErr="SetValue:請(qǐng)先執(zhí)行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];          //根據(jù)當(dāng)前字段的類型生成相應(yīng)的新值          if($this->sValue!="#@!")           $this->sValue.=",";          else           $this->sValue="";          $ftype=@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."類型目前版本不支持,請(qǐng)用別的方法添加數(shù)據(jù)!";             return;          }          if($this->NewEdit==2)           $this->sName.="=".$this->sEdit;    }   function Update()  //存儲(chǔ)新值到數(shù)據(jù)庫(kù)   {    $strSQL="";    if($this->NewEdit==0)    {     $this->nErr=1;     $this->sErr="Update:請(qǐng)先執(zhí)行AddNew()或者Edit(),再用SetValue()添加值!";     return;    }    if(empty($this->sValue))    {     $this->nErr=1;     $this->sErr="Update:在數(shù)據(jù)為空的情況下,不能添加或修改數(shù)據(jù)!";     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語(yǔ)句出錯(cuò),請(qǐng)檢查!";       return;    }    $this->sSQL=$strSQL;    if(!$this->nResult=mysql_query($strSQL))    {     $this->nErr=1;     $this->sErr="Update:SQL語(yǔ)句:".$strSQL."<br><br>MySql錯(cuò)誤:".mysql_error();     return;    }     //echo($this->sSQL."<br>");    //作清理工作    $this->NewEdit=0;    unset($this->aNew);    mysql_query("commit");   }  } ?> 

以上所述是小編給大家介紹的mysql仿asp的數(shù)據(jù)庫(kù)操作類,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 赞皇县| 漳浦县| 介休市| 昌都县| 得荣县| 周至县| 庆安县| 四会市| 衡阳市| 武城县| 上栗县| 恩平市| 习水县| 鄂尔多斯市| 太仆寺旗| 沁水县| 肥乡县| 郸城县| 鄂托克旗| 庄河市| 盐山县| 罗江县| 福海县| 南华县| 彭州市| 清苑县| 平潭县| 田阳县| 农安县| 景宁| 类乌齐县| 宜良县| 肃南| 高密市| 兴城市| 四子王旗| 鹤壁市| 桂林市| 福贡县| 沿河| 蕲春县|