侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

mysql仿asp的数据库操作类

数据库  /  管理员 发布于 7年前   165

具体代码如下所示:

<?php class MySQLDB  {   //MYSQL数据库操作类   //作者:熊毅   //版本:2.0(发行版)   //可以自由转载,修改请通知我[email protected]   //转载请保留以上声明     //上进行操作,当然也可以每次指定特殊的表进行操作   //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(!@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="") //标志开始添加数据   {    $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="";          $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");   }  } ?> 

以上所述是小编给大家介绍的mysql仿asp的数据库操作类,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言!


  • 上一条:
    HTML5 3D模型-百行代码实现旋转立体魔方实例
    下一条:
    Linux下mysql数据库的创建导入导出 及一些基本指令
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客