搜索
首页数据库mysql教程mysql仿asp的数据库操作类

mysql仿asp的数据库操作类

CODE:[复制到剪切板]class MySQLDB
{
//MYSQL数据库操作类
//作者:熊毅
//版本:2.0(发行版)

//可以自由转载,修改请通知我scxy78@yeah.net
//转载请保留以上声明

//上进行操作,当然也可以每次指定特殊的表进行操作
//nErr指示是否操作出错,sErr记录最后一次出错的错误代码,记录了明确的有哪个函数引起的错误
//错误之处请指正
//欢迎来信与我交流编程经验:scxy78@yeah.net
//我的CSDN:用户号:scxy;呢称:小熊,请多关照

//可以自由转载,修改请通知我scxy78@yeah.net
//转载请保留以上声明

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."

");
}
else
{
echo("没有错误

");
}
}

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."
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."
MySql错误:".mysql_error()."
";
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;$inCols;$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;$inCols;$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."

MySql错误:".mysql_error();
return;
}
$this->nCols=mysql_num_fields($this->nResult);
unset($this->aFName);
$this->aFName=array();
for($i=0;$inCols;$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."

MySql错误:".mysql_error();
return;
}
$this->nCols=mysql_num_fields($this->nResult);
unset($this->aFName);
$this->aFName=array();
for($i=0;$inCols;$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$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;$inCols;$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."
");

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."

MySql错误:".mysql_error();
return;
}
//echo($this->sSQL."
");
//作清理工作
$this->NewEdit=0;
unset($this->aNew);
mysql_query("commit");
}
}
?>
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
图文详解mysql架构原理图文详解mysql架构原理May 17, 2022 pm 05:54 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于架构原理的相关内容,MySQL Server架构自顶向下大致可以分网络连接层、服务层、存储引擎层和系统文件层,下面一起来看一下,希望对大家有帮助。

mysql怎么替换换行符mysql怎么替换换行符Apr 18, 2022 pm 03:14 PM

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

mysql怎么去掉第一个字符mysql怎么去掉第一个字符May 19, 2022 am 10:21 AM

方法:1、利用right函数,语法为“update 表名 set 指定字段 = right(指定字段, length(指定字段)-1)...”;2、利用substring函数,语法为“select substring(指定字段,2)..”。

mysql的msi与zip版本有什么区别mysql的msi与zip版本有什么区别May 16, 2022 pm 04:33 PM

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

mysql怎么将varchar转换为int类型mysql怎么将varchar转换为int类型May 12, 2022 pm 04:51 PM

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

MySQL复制技术之异步复制和半同步复制MySQL复制技术之异步复制和半同步复制Apr 25, 2022 pm 07:21 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

带你把MySQL索引吃透了带你把MySQL索引吃透了Apr 22, 2022 am 11:48 AM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

mysql怎么判断是否是数字类型mysql怎么判断是否是数字类型May 16, 2022 am 10:09 AM

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前By尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
1 个月前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)