mysql仿asp的数据库操作类

  • A+
所属分类:数据库

使用说明: 该类完全按照ADO的习惯书写的,用过ASP的人都觉得ASP连接数据库比PHP好用(这是我的感觉), 但PHP得一个一个API地写,挺累,该类做了完全的封装 创建类的实例时可以指定一个数据库表和选择的数据库,如:new MySQLDB("table","database");

  1. <?php
  2. class MySQLDB
  3.  {
  4.   //MYSQL数据库操作类 
  5.   //作者:熊毅 
  6.   //版本:2.0(发行版) 
  7.    查询数据时Query后可以用GetValue得到相应的值,既可以是字段名也可以是已0开始的序号
  8. 插入新值,先用AddNew后使用SetValue相应的字段名或序号和字段值,在用Update添加
  9. 编辑时用Edit指定编辑记录的条件在使用SetValue,最后用Update添加
  10. 在类使用过程中,sTName记录上次使用的数据库表名,当指定后可以直接使用,以后的操作默认在该表
  11.   //可以自由转载,修改请通知我scxy78@yeah.net 
  12.   //转载请保留以上声明 
  13.     //上进行操作,当然也可以每次指定特殊的表进行操作 
  14.   //nErr指示是否操作出错,sErr记录最后一次出错的错误代码,记录了明确的有哪个函数引起的错误 
  15.   //错误之处请指正 
  16.   //欢迎来信与我交流编程经验:scxy78@yeah.net 
  17.   //我的CSDN:用户号:scxy;呢称:小熊,请多关照 
  18.   //可以自由转载,修改请通知我scxy78@yeah.net 
  19.   //转载请保留以上声明 
  20.   var $host="localhost";    //主机名 
  21.   var $user="boot";      //用户名 
  22.   var $password="oaserver";  //用户密码 
  23.   var $linkid;         //连接值 
  24.   var $dbid;          //数据库选择的结果值 
  25.   var $sTName;         //指定当前操作的数据库表 
  26.   var $sErr;          //错误代码 
  27.   var $nErr;          //指示是否有错误存在,0无错误,1有错误 
  28.   var $nResult;        //查询结果值 
  29.   var $aFName;         //保存FieldsName的数组 
  30.   var $nRows;         //查询结果中的行数 
  31.   var $nCols;         //查询结果中的列数 
  32.   var $aNew;          //添加在AddNew函数后的数据,以数组形式保存 
  33.   var $NewEdit;         //判断当前是否在进行添加操作,0表示没有,1表示在进行添加,2表示编辑 
  34.   var $sEditCon;        //指定编辑记录的条件 
  35.   var $nOffset;        //记录偏移量 
  36.   var $EOF;           //标记是否到记录集尾 
  37.   var $sSQL;          //最后一条执行的SQL语句 
  38.   //执行Update所要用到的全局变量 
  39.   var $sName;          //字段名 
  40.   var $sValue;         //字段值AddNew时用 
  41.   var $sEdit;          //字段值Edit时用 
  42.   function Initialize()
  43.   {
  44.    $this->nErr=0;
  45.    $this->NewEdit=0;
  46.    $this->nResult=-1;
  47.    $this->nCols=0;
  48.    $this->nRows=0;
  49.    $this->nOffset=0;
  50.    $this->EOF=true;
  51.    $this->sName="";
  52.    $this->sValue="#@!";
  53.    $this->sEdit="#@!";
  54.    unset($this->aFName);
  55.    unset($this->aNew);
  56.   }
  57.   function MySqlDB($TableName="",$database="slt"//构造函数 
  58.   {
  59.    $this->Initialize();
  60.    $this->sTName=$TableName;
  61.    $this->linkid=mysql_connect($host,$user,$password);
  62.    if(!$this->linkid)
  63.    {
  64.     $this->nErr=1;
  65.     $this->sErr="MySqlDB:数据库连接出错,请启动服务!";
  66.     return;
  67.    }
  68.    $this->dbid=mysql_select_db($database);
  69.    if(!$this->dbid)
  70.    {
  71.     $this->nErr=1;
  72.     $this->sErr="MySqlDB:选择的数据库".$database."不存在!";
  73.     return;
  74.    }
  75.   }
  76.   function IsEmpty($Value)
  77.   {
  78.       if(is_string($Value)&&empty($Value))
  79.         return true;
  80.       return false;
  81.   }
  82.   function Destroy()     //数据清除处理 
  83.   {
  84.    mysql_query("commit");
  85.    mysql_close();
  86.   }
  87.   function PrintErr()
  88.   {
  89.    if($this->nErr==1)
  90.    {
  91.     echo($this->sErr."<br><br>");
  92.    }
  93.    else
  94.    {
  95.     echo("没有错误<br><br>");
  96.    }
  97.   }
  98.     function Execute($SQL) //直接执行SQL语句 
  99.      {
  100.         if(empty($SQL))
  101.          {
  102.             $this->nErr=1;
  103.             $this->sErr="Execute:执行语句不能为空!";
  104.             return false;
  105.          }
  106.          $this->sSQL=$SQL;
  107.          if(!mysql_query($SQL))
  108.          {
  109.              $this->nErr=1;
  110.              $this->sErr="Execute:SQL语句:".$SQL."<br>MySql错误:".mysql_error();
  111.              return false;
  112.          }
  113.          return true;
  114.      }
  115.   function Query($TableName="",$SQL="*",$Condition="",$Order="",$Sequenc=""//在数据库里执行查询 
  116.   {
  117.    $this->Initialize();
  118.    if(!empty($TableName))
  119.     $this->sTName=$TableName;
  120.    $strSQL="select ".$SQL." from ".$this->sTName;
  121.    if(!empty($Condition))
  122.     $strSQL=$strSQL." where ".$Condition;
  123.    if(!empty($Order))
  124.     $strSQL=$strSQL." order by ".$Order;
  125.    if(!empty($Sequenc))
  126.     $strSQL=$strSQL." ".$Sequenc;
  127.      $this->sSQL=$strSQL;
  128.    if(!$this->nResult=mysql_query($strSQL))
  129.    {
  130.     $this->nErr=1;
  131.     $this->sErr="Query:SQL语句:".$strSQL."<br>MySql错误:".mysql_error()."<br>";
  132.     return;
  133.    }
  134.    $this->nOffset=0;
  135.    $this->nRows=mysql_num_rows($this->nResult);
  136.    $this->nCols=mysql_num_fields($this->nResult);
  137.      if($this->nRows>0)
  138.          $this->EOF=false;
  139.      else
  140.          $this->EOF=true;
  141.    unset($this->aFName);
  142.    $this->aFName=array();
  143.    for($i=0;$i<$this->nCols;$i++)
  144.      $this->aFName[$i]=strtolower(mysql_field_name($this->nResult,$i));
  145.   }
  146.     function MoveNext()
  147.      {
  148.         if($this->EOF)
  149.          {
  150.             $this->nErr=1;
  151.             $this->sErr="MoveNext:已经移到记录集末尾!";
  152.             return;
  153.          }
  154.         $this->nOffset++;
  155.         if($this->nOffset>=$this->nRows)
  156.             $this->EOF=true;
  157.      }
  158.    function MoveTo($Offset)
  159.    {
  160.     if(empty($Offset))
  161.     {
  162.      $this->nErr=1;
  163.      $this->sErr="MoveTo:必须指定偏移量! ";
  164.      return;
  165.     }
  166.     if(!$this->nResult)
  167.     {
  168.      $this->nErr=1;
  169.      $this->sErr="MoveTo:请先执行查询:Query";
  170.      return;
  171.     }
  172.     $this->nOffset=$Offset;
  173.    }
  174.   //得到指定行的指定列的值,返回字符串 
  175.   //如果不指定Offset将取得下一行的值 
  176.   //如果不指定nFields将取得该行的值,并已数组形式返回 
  177.   function GetValue($nFields=-1,$Offset=-1)
  178.   {
  179.    if($this->nResult==-1)
  180.    {
  181.     $this->nErr=1;
  182.     $this->sErr="GetValue:请先执行Query()函数!";
  183.     return;
  184.    }
  185.    if($Offset>-1)
  186.    {
  187.         $this->nOffset=$Offset;
  188.     if($this->nOffset>=$this->nRows)
  189.     {
  190.      $this->nErr=1;
  191.      $this->sErr="GetValue:所要求的偏移量太大,无法达到!";
  192.      return;
  193.     }
  194.    }
  195.       if(!@mysql_data_seek($this->nResult,$this->nOffset))
  196.         {
  197.          $this->nErr=1;
  198.          $this->sErr="GetValue:请求不存在的记录!";
  199.          return;
  200.         }
  201.    $aResult=mysql_fetch_row($this->nResult);
  202.    if(is_int($nFields)&&$nFields>-1)
  203.    {
  204.     if($nFileds>$this->nCols)
  205.     {
  206.      $this->nErr=1;
  207.      $this->sErr="GetValue:所请求的列值大于实际的列值!";
  208.      return;
  209.     }
  210.     return $aResult[$nFields];
  211.    }
  212.      if(is_string($nFields))
  213.      {
  214.         $nFields=strtolower($nFields);
  215.       for($i=0;$i<$this->nCols;$i++)
  216.         {
  217.          if($this->aFName[$i]==$nFields)
  218.              break;
  219.         }
  220.         if($i==$this->nCols)
  221.          {
  222.             $this->nErr=1;
  223.             $this->sErr="GetValue:所请求的列不存在,请仔细检查!";
  224.             return;
  225.          }
  226.          return $aResult[$i];
  227.      }
  228.    return $aResult;
  229.   }
  230.   function AddNew($TableName=""//标志开始添加数据 
  231.   {
  232.    $this->Initialize();
  233.    if(!empty($TableName))
  234.     $this->sTName=$TableName;
  235.    if($this->NewEdit>0)
  236.    {
  237.     $this->nErr=1;
  238.     $this->sErr="AddNew:你正在对数据库进行添加或更新操作!";
  239.     return;
  240.    }
  241.    if(empty($this->sTName))
  242.    {
  243.     $this->nErr=1;
  244.     $this->sErr="AddNew:想要添加的数据库表为空,可以在构造时指定,也可在AddNew()时指定!";
  245.     return;
  246.    }
  247.    unset($this->aNew);
  248.    $this->aNew=array();
  249.    $this->NewEdit=1;
  250.    $strSQL="select * from ".$this->sTName;
  251.      $this->sSQL=$strSQL;
  252.    if(!$this->nResult=mysql_query($strSQL))
  253.    {
  254.     $this->nErr=1;
  255.     $this->sErr="AddNew:SQL语句:".strSQL."<br><br>MySql错误:".mysql_error();
  256.     return;
  257.    }
  258.    $this->nCols=mysql_num_fields($this->nResult);
  259.    unset($this->aFName);
  260.    $this->aFName=array();
  261.    for($i=0;$i<$this->nCols;$i++)
  262.      $this->aFName[$i]=strtolower(mysql_field_name($this->nResult,$i));
  263.   }
  264.   function Edit($Condition="",$TableName=""//对指定数据库表进行编辑 
  265.   {
  266.          $this->Initialize();
  267.          if(!empty($TableName))
  268.              $this->sTName=$TableName;
  269.          $this->sEditCon=$Condition;
  270.          if(empty($this->sTName))
  271.          {
  272.              $this->nErr=1;
  273.              $this->sErr="Edit:在编辑前请先指定数据库表!";
  274.              return;
  275.          }
  276.          unset($this->aNew);
  277.          $this->aNew=array();
  278.          $this->NewEdit=2;
  279.          $strSQL="select * from ".$this->sTName;
  280.          $this->sSQL=$strSQL;
  281.          if(!$this->nResult=mysql_query($strSQL))
  282.      {
  283.        $this->nErr=1;
  284.        $this->sErr="Edit:SQL语句:".strSQL."<br><br>MySql错误:".mysql_error();
  285.        return;
  286.      }
  287.      $this->nCols=mysql_num_fields($this->nResult);
  288.      unset($this->aFName);
  289.      $this->aFName=array();
  290.      for($i=0;$i<$this->nCols;$i++)
  291.        $this->aFName[$i]=strtolower(mysql_field_name($this->nResult,$i));
  292.   }
  293.   function SetValue($Index,$Value) //指定数据,跟在AddNew后执行; 
  294.   {
  295.        if($this->NewEdit==0)
  296.        {
  297.         $this->nErr=1;
  298.         $this->sErr="SetValue:请先执行AddNew()或者Edit()!";
  299.         return;
  300.        }
  301.        if(is_int($Index))
  302.        {
  303.          if($Index<0||$Index>$this->nCols)
  304.          {
  305.           $this->nErr=1;
  306.           $this->sErr="SetValue:插入不存在的列值!";
  307.           return;
  308.          }
  309.          $this->aNew[$Index]=$Value;
  310.          $tmpIn=$Index;
  311.        }
  312.        elseif(is_string($Index))
  313.        {
  314.         $Index=strtolower($Index);
  315.         for($i=0;$i<$this->nCols;$i++)
  316.         {
  317.           if($this->aFName[$i]==$Index)
  318.             break;
  319.         }
  320.         if($i==$this->nCols)
  321.         {
  322.           $this->nErr=1;
  323.           $this->sErr="SetValue:插入不存在的列值!";
  324.           return;
  325.          }
  326.          $this->aNew[$i]=$Value;
  327.          $tmpIn=$i;
  328.        }
  329.          if(!empty($this->sName))
  330.           $this->sName.=",";
  331.          $this->sName.=$this->aFName[$tmpIn];
  332.          //根据当前字段的类型生成相应的新值 
  333.          if($this->sValue!="#@!")
  334.           $this->sValue.=",";
  335.          else
  336.           $this->sValue="";
  337.          $ftype=@mysql_field_type($this->nResult,$i);
  338.          //echo($ftype.",".$this->aNew[$i].",".$i.":".$sValue."<br>"); 
  339.          switch($ftype)
  340.          {
  341.          case "string":
  342.          case "date":
  343.          case "datetime":
  344.             $this->sValue.=""".$this->aNew[$tmpIn].""";
  345.             $this->sEdit=""".$this->aNew[$tmpIn].""";
  346.             break;
  347.          case "int":
  348.          case "unknown":
  349.             $this->sValue.=$this->aNew[$tmpIn];
  350.             $this->sEdit=$this->aNew[$tmpIn];
  351.             break;
  352.          default:
  353.             $this->nErr=1;
  354.             $this->sErr="Update:字段名为".$this->aFName[$tmpIn]."的".$ftype."类型目前版本不支持,请用别的方法添加数据!";
  355.             return;
  356.          }
  357.          if($this->NewEdit==2)
  358.           $this->sName.="=".$this->sEdit;
  359.    }
  360.   function Update()  //存储新值到数据库 
  361.   {
  362.    $strSQL="";
  363.    if($this->NewEdit==0)
  364.    {
  365.     $this->nErr=1;
  366.     $this->sErr="Update:请先执行AddNew()或者Edit(),再用SetValue()添加值!";
  367.     return;
  368.    }
  369.    if(empty($this->sValue))
  370.    {
  371.     $this->nErr=1;
  372.     $this->sErr="Update:在数据为空的情况下,不能添加或修改数据!";
  373.     return;
  374.    }
  375.    switch($this->NewEdit)
  376.    {
  377.     case 1:    //添加 
  378.       $strSQL="insert into ";
  379.       $strSQL.=$this->sTName;
  380.       $strSQL.=" (".$this->sName.") ";
  381.       $strSQL.="values (".$this->sValue.")";
  382.       break;
  383.    case 2:     //修改 
  384.       $strSQL="update ";
  385.       $strSQL.=$this->sTName;
  386.       $strSQL.=" set ";
  387.       $strSQL.=$this->sName;
  388.       if(!empty($this->sEditCon))
  389.         $strSQL.=" where ".$this->sEditCon;
  390.       break;
  391.    default:
  392.       $this->nErr=1;
  393.       $this->sErr="Update:Update()生成SQL语句出错,请检查!";
  394.       return;
  395.    }
  396.    $this->sSQL=$strSQL;
  397.    if(!$this->nResult=mysql_query($strSQL))
  398.    {
  399.     $this->nErr=1;
  400.     $this->sErr="Update:SQL语句:".$strSQL."<br><br>MySql错误:".mysql_error();
  401.     return;
  402.    }
  403.     //echo($this->sSQL."<br>"); 
  404.    //作清理工作 
  405.    $this->NewEdit=0;
  406.    unset($this->aNew);
  407.    mysql_query("commit");
  408.   }
  409.  }
  410. ?>
图片引用自网络