首页 > 数据库 > MySQL > 正文

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

2024-07-24 12:54:48
字体:
来源:转载
供稿:网友

 

 

 

 


  class mysqldb
  {
    //mysql数据库操作类
    //作者:熊毅
    //版本:2.0(发行版)
   
    //可以自由转载,修改请通知我[email protected]
    //转载请保留以上声明
   
    //使用说明:
    //该类完全按照ado的习惯书写的,用过asp的人都觉得asp连接数据库比php好用(这是我的感觉),
    //但php得一个一个api地写,挺累,该类做了完全的封装
    //创建类的实例时可以指定一个数据库表和选择的数据库,如:new mysqldb("table","database");
    //查询数据时query后可以用getvalue得到相应的值,既可以是字段名也可以是已0开始的序号
    //插入新值,先用addnew后使用setvalue相应的字段名或序号和字段值,在用update添加
    //编辑时用edit指定编辑记录的条件在使用setvalue,最后用update添加
    //在类使用过程中,stname记录上次使用的数据库表名,当指定后可以直接使用,以后的操作默认在该表
    //上进行操作,当然也可以每次指定特殊的表进行操作
    //nerr指示是否操作出错,serr记录最后一次出错的错误代码,记录了明确的有哪个函数引起的错误
    //错误之处请指正
    //欢迎来信与我交流编程经验:[email protected]
    //我的csdn:用户号:scxy;呢称:小熊,请多关照
   
    //可以自由转载,修改请通知我[email protected]
    //转载请保留以上声明

    var $host="localhost";        //主机名
    var $user="boot";           //用户名
    var $password="oaserver";   //用户密码
    var $linkid;                 //连接值
    var $dbid;                   //数据库选择的结果值
    var $stname;                  //指定当前操作的数据库表
    var $serr;                   //错误代码
    var $nerr;                   //指示是否有错误存在,0无错误,1有错误
    var $nresult;                //查询结果值
    var $afname;                 //保存fieldsname的数组
    var $nrows;                  //查询结果中的行数
    var $ncols;                  //查询结果中的列数
    var $anew;                   //添加在addnew函数后的数据,以数组形式保存
    var $newedit;                  //判断当前是否在进行添加操作,0表示没有,1表示在进行添加,2表示编辑
    var $seditcon;               //指定编辑记录的条件
    var $noffset;                //记录偏移量
    var $eof;                     //标记是否到记录集尾
    var $ssql;                    //最后一条执行的sql语句

    //执行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")  //构造函数
    {
      $this->initialize();
      $this->stname=$tablename;
      $this->linkid=mysql_connect($host,$user,$password);
      if(!$this->linkid)
      {
        $this->nerr=1;
        $this->serr="mysqldb:数据库连接出错,请启动服务!";
        return;
      }
      $this->dbid=mysql_select_db($database);
      if(!$this->dbid)
      {
        $this->nerr=1;
        $this->serr="mysqldb:选择的数据库".$database."不存在!";
        return;
      }
    }

    function isempty($value)
    {
            if(is_string($value)&&empty($value))
               return true;
            return false;
    }

    function destroy()          //数据清除处理
    {
      mysql_query("commit");
      mysql_close();
    }

    function printerr()
    {
      if($this->nerr==1)
      {
        echo($this->serr."<br><br>");
      }
      else
      {
        echo("没有错误<br><br>");
      }
    }

        function execute($sql)  //直接执行sql语句
          {
                if(empty($sql))
                  {
                        $this->nerr=1;
                        $this->serr="execute:执行语句不能为空!";
                        return false;
                  }
                 $this->ssql=$sql;
                  if(!mysql_query($sql))
                  {
                          $this->nerr=1;
                          $this->serr="execute:sql语句:".$sql."<br>mysql错误:".mysql_error();
                          return false;
                  }
                  return true;
          }

    function query($tablename="",$sql="*",$condition="",$order="",$sequenc="") //在数据库里执行查询
    {
      $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."<br>mysql错误:".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:已经移到记录集末尾!";
                        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:请先执行查询:query";
          return;
        }
        $this->noffset=$offset;
     }

    //得到指定行的指定列的值,返回字符串
    //如果不指定offset将取得下一行的值
    //如果不指定nfields将取得该行的值,并已数组形式返回
    function getvalue($nfields=-1,$offset=-1)
    {
      if($this->nresult==-1)
      {
        $this->nerr=1;
        $this->serr="getvalue:请先执行query()函数!";
        return;
      }
      if($offset>-1)
      {
                $this->noffset=$offset;
        if($this->noffset>=$this->nrows)
        {
          $this->nerr=1;
          $this->serr="getvalue:所要求的偏移量太大,无法达到!";
          return;
        }
      }
           if([email protected]_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="")  //标志开始添加数据
    {
      $this->initialize();
      if(!empty($tablename))
        $this->stname=$tablename;
      if($this->newedit>0)
      {
        $this->nerr=1;
        $this->serr="addnew:你正在对数据库进行添加或更新操作!";
        return;
      }
      if(empty($this->stname))
      {
        $this->nerr=1;
        $this->serr="addnew:想要添加的数据库表为空,可以在构造时指定,也可在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."<br><br>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="")  //对指定数据库表进行编辑
    {
                  $this->initialize();
                  if(!empty($tablename))
                          $this->stname=$tablename;
                  $this->seditcon=$condition;
                  if(empty($this->stname))
                  {
                          $this->nerr=1;
                          $this->serr="edit:在编辑前请先指定数据库表!";
                          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."<br><br>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) //指定数据,跟在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="";
                 $[email protected]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");
    }
  }
?>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表