mysql mimics asp's database operation class

  • 2020-05-06 11:49:15
  • OfStack


<?php 
class MySQLDB 
 { 
  //MYSQL Database operation class  
  // Author: xiong yi  
  // Version: 2.0( release ) 
    When querying data Query Can be used after GetValue Get the corresponding value, which can be either a field name or an already 0 The starting number  
 Insert a new value, use first AddNew After using SetValue Corresponding field name or serial number and field value, in use Update add  
 Used when editing Edit Specifies the conditions for editing records in use SetValue In the end, Update add  
 During class usage, sTName Record the last database table name used, which can be used directly when specified, and which is the default for future operations  

  // Can freely reprint, modify please inform me scxy78@yeah.net 
  // Reprint please retain the above statement  
   
    // You can also specify a special table each time  
  //nErr Indicates whether there is an error in operation, sErr The error code that records the last time something went wrong records exactly which function caused the error  
  // Please correct the mistakes  
  // Welcome to exchange programming experience with me: scxy78@yeah.net 
  // my CSDN User number: scxy ; Little bear, please take care of it  
   
  // Can freely reprint, modify please inform me scxy78@yeah.net 
  // Reprint please retain the above statement  

  var $host="localhost";    // The host name  
  var $user="boot";      // The user name  
  var $password="oaserver";  // The user password  
  var $linkid;         // Connect the value  
  var $dbid;          // The result value selected by the database  
  var $sTName;         // Specifies the database table for the current operation  
  var $sErr;          // The error code  
  var $nErr;          // Indicates whether an error exists ,0 No error ,1 There is an error  
  var $nResult;        // Query result value  
  var $aFName;         // save FieldsName An array of  
  var $nRows;         // The number of rows in the query result  
  var $nCols;         // The number of columns in the query result  
  var $aNew;          // Add in AddNew The data after the function is stored as an array  
  var $NewEdit;         // To determine whether the add operation is currently in progress, 0 No, 1 Means adding ,2 Said the editor  
  var $sEditCon;        // Specifies conditions for editing records  
  var $nOffset;        // Record offset  
  var $EOF;           // Mark whether to end the recordset  
  var $sSQL;          // The last one is executed SQL statements  

  // perform Update The global variable to use  
  var $sName;          // The field name  
  var $sValue;         // The field values AddNew When using  
  var $sEdit;          // The field values Edit When using  

  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") // The constructor  
  { 
   $this->Initialize(); 
   $this->sTName=$TableName; 
   $this->linkid=mysql_connect($host,$user,$password); 
   if(!$this->linkid) 
   { 
    $this->nErr=1; 
    $this->sErr="MySqlDB: Database connection error. Please start the service !"; 
    return; 
   } 
   $this->dbid=mysql_select_db($database); 
   if(!$this->dbid) 
   { 
    $this->nErr=1; 
    $this->sErr="MySqlDB: Selected database ".$database." There is no !"; 
    return; 
   } 
  } 

  function IsEmpty($Value) 
  { 
      if(is_string($Value)&&empty($Value)) 
        return true; 
      return false; 
  } 

  function Destroy()     // Data clearing  
  { 
   mysql_query("commit"); 
   mysql_close(); 
  } 

  function PrintErr() 
  { 
   if($this->nErr==1) 
   { 
    echo($this->sErr."<br><br>"); 
   } 
   else 
   { 
    echo(" There is no error <br><br>"); 
   } 
  } 

    function Execute($SQL) // Direct execution SQL statements  
     { 
        if(empty($SQL)) 
         { 
            $this->nErr=1; 
            $this->sErr="Execute: Execute statement cannot be empty! "; 
            return false; 
         } 
         $this->sSQL=$SQL; 
         if(!mysql_query($SQL)) 
         { 
             $this->nErr=1; 
             $this->sErr="Execute:SQL Statement: ".$SQL."<br>MySql Error: ".mysql_error(); 
             return false; 
         } 
         return true; 
     } 

  function Query($TableName="",$SQL="*",$Condition="",$Order="",$Sequenc="") // Execute the query in the database  
  { 
   $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 Statement: ".$strSQL."<br>MySql Error: ".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: Has moved to the end of the recordset! "; 
            return; 
         } 
        $this->nOffset++; 
        if($this->nOffset>=$this->nRows) 
            $this->EOF=true; 
     } 

   function MoveTo($Offset) 
   { 
    if(empty($Offset)) 
    { 
     $this->nErr=1; 
     $this->sErr="MoveTo: You must specify an offset ! "; 
     return; 
    } 

    if(!$this->nResult) 
    { 
     $this->nErr=1; 
     $this->sErr="MoveTo: Please execute the query first :Query"; 
     return; 
    } 
    $this->nOffset=$Offset; 
   } 

  // Gets the value of the specified column for the specified row, returns a string  
  // If you don't specify Offset Gets the value of the next row  
  // If you don't specify nFields Gets the value of that row and returns it as an array  
  function GetValue($nFields=-1,$Offset=-1) 
  { 
   if($this->nResult==-1) 
   { 
    $this->nErr=1; 
    $this->sErr="GetValue: Please perform Query() The function! "; 
    return; 
   } 
   if($Offset>-1) 
   { 
        $this->nOffset=$Offset; 
    if($this->nOffset>=$this->nRows) 
    { 
     $this->nErr=1; 
     $this->sErr="GetValue: The required offset is too large to achieve! "; 
     return; 
    } 
   } 
      if(!@mysql_data_seek($this->nResult,$this->nOffset)) 
        { 
         $this->nErr=1; 
         $this->sErr="GetValue: Request a record that does not exist! "; 
         return; 
        } 
   $aResult=mysql_fetch_row($this->nResult); 
   if(is_int($nFields)&&$nFields>-1) 
   { 
    if($nFileds>$this->nCols) 
    { 
     $this->nErr=1; 
     $this->sErr="GetValue: The requested column value is greater than the actual column value! "; 
     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: The requested column does not exist, please check it carefully! "; 
            return; 
         } 
         return $aResult[$i]; 
     } 
   return $aResult; 
  } 

  function AddNew($TableName="") // The flag begins to add data  
  { 
   $this->Initialize(); 
   if(!empty($TableName)) 
    $this->sTName=$TableName; 
   if($this->NewEdit>0) 
   { 
    $this->nErr=1; 
    $this->sErr="AddNew: You are adding or updating the database! "; 
    return; 
   } 
   if(empty($this->sTName)) 
   { 
    $this->nErr=1; 
    $this->sErr="AddNew: The database table you want to add is empty and can be specified at build time or in AddNew() When specified. "; 
    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 Statement: ".strSQL."<br><br>MySql Error: ".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="") // Edits the specified database table  
  { 
         $this->Initialize(); 
         if(!empty($TableName)) 
             $this->sTName=$TableName; 
         $this->sEditCon=$Condition; 
         if(empty($this->sTName)) 
         { 
             $this->nErr=1; 
             $this->sErr="Edit: Please specify the database table before editing! "; 
             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 Statement: ".strSQL."<br><br>MySql Error: ".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) // Specify the data to follow AddNew After the implementation ; 
  { 
       if($this->NewEdit==0) 
       { 
        $this->nErr=1; 
        $this->sErr="SetValue: Please perform AddNew() or Edit() ! "; 
        return; 
       } 
       if(is_int($Index)) 
       { 
         if($Index<0||$Index>$this->nCols) 
         { 
          $this->nErr=1; 
          $this->sErr="SetValue: Insert nonexistent column values! "; 
          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: Insert nonexistent column values! "; 
          return; 
         } 
         $this->aNew[$i]=$Value; 
         $tmpIn=$i; 
       } 
         if(!empty($this->sName)) 
          $this->sName.=","; 
         $this->sName.=$this->aFName[$tmpIn]; 
         // Generates the corresponding new value based on the type of the current field  
         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: Field called ".$this->aFName[$tmpIn]." the ".$ftype." The current version of the type is not supported, please use another method to add data! "; 
            return; 
         } 

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

  function Update()  // Store the new value to the database  
  { 
   $strSQL=""; 

   if($this->NewEdit==0) 
   { 
    $this->nErr=1; 
    $this->sErr="Update: Please perform AddNew() or Edit() And use it to SetValue() Add value! "; 
    return; 
   } 

   if(empty($this->sValue)) 
   { 
    $this->nErr=1; 
    $this->sErr="Update: You cannot add or modify data when it is empty! "; 
    return; 
   } 

   switch($this->NewEdit) 
   { 
    case 1:    // add  
      $strSQL="insert into "; 
      $strSQL.=$this->sTName; 
      $strSQL.=" (".$this->sName.") "; 
      $strSQL.="values (".$this->sValue.")"; 
      break; 
   case 2:     // Modify the  
      $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() generate SQL Statement error, please check! "; 
      return; 
   } 

   $this->sSQL=$strSQL; 
   if(!$this->nResult=mysql_query($strSQL)) 
   { 
    $this->nErr=1; 
    $this->sErr="Update:SQL Statement: ".$strSQL."<br><br>MySql Error: ".mysql_error(); 
    return; 
   } 
    //echo($this->sSQL."<br>"); 
   // Clean up  
   $this->NewEdit=0; 
   unset($this->aNew); 
   mysql_query("commit"); 
  } 
 } 
?>


Related articles: