正 文

MYSQL的操作类(已封装)


www.7dspace.com  更新日期:2005-11-3 6:57:54  七度空间


   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="";
                 $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."类型目前版本不支持,请用别的方法添加数据!";
                       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");
    }
  }

请将全角<>换成半角

6页,页码:[1] [2] [3] [4] [5] [6] 

上一篇:使用XML-RPC来访问Web服务
下一篇:Windows系统自动保存密码再现
作者:熊毅(小熊)  来源:CSDN ( 责任编辑:7dspace )
收藏此页】【打印】【关闭
站 内 搜 索
 

热 点 导 读
特 别 推 荐