久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

MYSQL修改后新版本的操作類是怎樣的

193次閱讀
沒有評論

共計 7562 個字符,預計需要花費 19 分鐘才能閱讀完成。

MYSQL 修改后新版本的操作類是怎樣的,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面丸趣 TV 小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

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

  class Mysqldb
  {
 
 
  // 使用說明:
  // 該類完全按照 ADO 的習慣書寫的,用過 ASP 的人都覺得 ASP 連接數(shù)據(jù)庫比 PHP 好用(這是我的感覺),
  // 但 PHP 得一個一個 api 地寫,挺累,該類做了完全的封裝
  // 創(chuàng)建類的實例時可以指定一個數(shù)據(jù)庫表和選擇的數(shù)據(jù)庫,如:new MySQLDB(table , database
  // 查詢數(shù)據(jù)時 Query 后可以用 GetValue 得到相應的值,既可以是字段名也可以是已 0 開始的序號
  // 插入新值,先用 AddNew 后使用 SetValue 相應的字段名或序號和字段值,在用 Update 添加
  // 編輯時用 Edit 指定編輯記錄的條件在使用 SetValue,最后用 Update 添加
  // 在類使用過程中,sTName 記錄上次使用的數(shù)據(jù)庫表名,當指定后可以直接使用,以后的操作默認在該表
  // 上進行操作,當然也可以每次指定特殊的表進行操作
  //nErr 指示是否操作出錯,sErr 記錄最后一次出錯的錯誤代碼,記錄了明確的有哪個函數(shù)引起的錯誤

   var $host= localhost   // 主機名
  var $user= boot   // 用戶名
  var $password= oaserver   // 用戶密碼
  var $linkid;  // 連接值
  var $dbid;  // 數(shù)據(jù)庫選擇的結果值
  var $sTName;  // 指定當前操作的數(shù)據(jù)庫表
  var $sErr;  // 錯誤代碼
  var $nErr;  // 指示是否有錯誤存在,0 無錯誤,1 有錯誤
  var $nResult;  // 查詢結果值
  var $aFName;  // 保存 FieldsName 的數(shù)組
  var $nRows;  // 查詢結果中的行數(shù)
  var $nCols;  // 查詢結果中的列數(shù)
  var $aNew;  // 添加在 AddNew 函數(shù)后的數(shù)據(jù),以數(shù)組形式保存
  var $NewEdit;  // 判斷當前是否在進行添加操作,0 表示沒有,1 表示在進行添加,2 表示編輯
  var $sEditCon;  // 指定編輯記錄的條件
  var $nOffset;  // 記錄偏移量
  var $EOF;  // 標記是否到記錄集尾
  var $sSQL;  // 最后一條執(zhí)行的 SQL 語句

  // 執(zhí)行 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)  // 構造函數(shù)
  {
  $this- Initialize();
  $this- sTName=$TableName;
  $this- linkid=mysql_connect($host,$user,$password);
  if(!$this- linkid)
  {
  $this- nErr=1;
  $this- sErr= MySqlDB: 數(shù)據(jù)庫連接出錯,請啟動服務!
  return;
  }
  $this- dbid=mysql_select_db($database);
  if(!$this- dbid)
  {
  $this- nErr=1;
  $this- sErr= MySqlDB: 選擇的數(shù)據(jù)庫 .$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.

  }
  else
  {
  echo(沒有錯誤

  }
  }

  function Execute($SQL)  // 直接執(zhí)行 SQL 語句
  {
  if(empty($SQL))
  {
  $this- nErr=1;
  $this- sErr= Execute: 執(zhí)行語句不能為空!
  return false;
  }
  $this- sSQL=$SQL;
  if(!mysql_query($SQL))
  {
  $this- nErr=1;
  $this- sErr= Execute:SQL 語句:.$SQL.
MySql 錯誤:.mysql_error();
  return false;
  }
  return true;
  }

  function Query($TableName= ,$SQL= * ,$Condition= ,$Order= ,$Sequenc=) // 在數(shù)據(jù)庫里執(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 語句:.$strSQL.
MySql 錯誤:.mysql_error().

  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: 請先執(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: 請先執(zhí)行 Query() 函數(shù)!
  return;
  }
  if($Offset -1)
  {
  $this- nOffset=$Offset;
  if($this- nOffset =$this- nRows)
  {
  $this- nErr=1;
  $this- sErr= GetValue: 所要求的偏移量太大,無法達到!
  return;
  }
  }
  if(!@mysql_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=)  // 標志開始添加數(shù)據(jù)
  {
  $this- Initialize();
  if(!empty($TableName))
  $this- sTName=$TableName;
  if($this- NewEdit 0)
  {
  $this- nErr=1;
  $this- sErr= AddNew: 你正在對數(shù)據(jù)庫進行添加或更新操作!
  return;
  }
  if(empty($this- sTName))
  {
  $this- nErr=1;
  $this- sErr= AddNew: 想要添加的數(shù)據(jù)庫表為空,可以在構造時指定,也可在 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.

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=)  // 對指定數(shù)據(jù)庫表進行編輯
  {
  $this- Initialize();
  if(!empty($TableName))
  $this- sTName=$TableName;
  $this- sEditCon=$Condition;
  if(empty($this- sTName))
  {
  $this- nErr=1;
  $this- sErr= Edit: 在編輯前請先指定數(shù)據(jù)庫表!
  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.

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) // 指定數(shù)據(jù),跟在 AddNew 后執(zhí)行;
  {
  if($this- NewEdit==0)
  {
  $this- nErr=1;
  $this- sErr= SetValue: 請先執(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ù)當前字段的類型生成相應的新值
  if($this- sValue!= #@!)
  $this- sValue.= ,
  else
  $this- sValue=
  $ftype=@mysql_field_type($this- nResult,$i);
  //echo($ftype. , .$this- aNew[$i]. , .$i. : .$sValue.

 
  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. 類型目前版本不支持,請用別的方法添加數(shù)據(jù)!
  return;
  }

  if($this- NewEdit==2)
  $this- sName.= = .$this- sEdit;
  }

  function Update()  // 存儲新值到數(shù)據(jù)庫
  {
  $strSQL=

  if($this- NewEdit==0)
  {
  $this- nErr=1;
  $this- sErr= Update: 請先執(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 語句出錯,請檢查!
  return;
  }

  $this- sSQL=$strSQL;
  if(!$this- nResult=mysql_query($strSQL))
  {
  $this- nErr=1;
  $this- sErr= Update:SQL 語句:.$strSQL.

MySql 錯誤:.mysql_error();
  return;
  }
  //echo($this- sSQL.

  // 作清理工作
  $this- NewEdit=0;
  unset($this- aNew);
  mysql_query(commit
  }
  }
?

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注丸趣 TV 行業(yè)資訊頻道,感謝您對丸趣 TV 的支持。

正文完
 
丸趣
版權聲明:本站原創(chuàng)文章,由 丸趣 2023-07-19發(fā)表,共計7562字。
轉(zhuǎn)載說明:除特殊說明外本站除技術相關以外文章皆由網(wǎng)絡搜集發(fā)布,轉(zhuǎn)載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 措勤县| 页游| 中阳县| 巴彦县| 吴川市| 蒙城县| 扬中市| 拉孜县| 邯郸市| 安平县| 铁岭县| 乐昌市| 双鸭山市| 繁昌县| 定陶县| 尼勒克县| 德惠市| 仪陇县| 仁怀市| 九龙县| 简阳市| 巫溪县| 凉城县| 景德镇市| 全南县| 潞城市| 广宁县| 专栏| 双鸭山市| 神木县| 平顶山市| 元阳县| 临清市| 凌云县| 台东县| 柳河县| 桐庐县| 福安市| 含山县| 弥勒县| 长汀县|