mysql仿asp的数据库操作类

CODE:[复制到剪切板]<?php
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 $nOff; //记录偏移量
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->nOff=0;
$this->EOF=true;
$this->sName="";
$this->sValue="#@!";
$this->sEdit="#@!";
un($this->aFName);
un($this->aNew);
}
function MySqlDB($TableName="",$database="slt") //构造
{
$this->Initialize;
$this->sTName=$TableName;
$this->linkid=mysql_connect($host,$user,$password);
(!$this->linkid)
{
$this->nErr=1;
$this->sErr="MySqlDB:数据库连接出错请启动服务!";
;
}
$this->dbid=mysql_select_db($database);
(!$this->dbid)
{
$this->nErr=1;
$this->sErr="MySqlDB:选择数据库".$database."不存在!";
;
}
}
function IsEmpty($Value)
{
(is_($Value)&&empty($Value))
true;
false;
}
function Destroy //数据清除处理
{
mysql_query("commit");
mysql_close;
}
function PrErr
{
($this->nErr1)
{
echo($this->sErr."<br><br>");
}

{
echo("没有<br><br>");
}
}
function Execute($SQL) //直接执行SQL语句
{
(empty($SQL))
{
$this->nErr=1;
$this->sErr="Execute:执行语句不能为空!";
false;
}
$this->sSQL=$SQL;
(!mysql_query($SQL))
{
$this->nErr=1;
$this->sErr="Execute:SQL语句:".$SQL."<br>MySql:".mysql_error;
false;
}
true;
}
function Query($TableName="",$SQL="*",$Condition="",$Order="",$Sequenc="") //在数据库里执行查询
{
$this->Initialize;
(!empty($TableName))
$this->sTName=$TableName;
$strSQL="select ".$SQL." from ".$this->sTName;
(!empty($Condition))
$strSQL=$strSQL." where ".$Condition;
(!empty($Order))
$strSQL=$strSQL." order by ".$Order;
(!empty($Sequenc))
$strSQL=$strSQL." ".$Sequenc;
$this->sSQL=$strSQL;
(!$this->nResult=mysql_query($strSQL))
{
$this->nErr=1;
$this->sErr="Query:SQL语句:".$strSQL."<br>MySql:".mysql_error."<br>";
;
}
$this->nOff=0;
$this->nRows=mysql_num_rows($this->nResult);
$this->nCols=mysql_num_fields($this->nResult);
($this->nRows>0)
$this->EOF=false;

$this->EOF=true;
un($this->aFName);
$this->aFName=.gif' />;
for($i=0;$i<$this->nCols;$i)
$this->aFName[$i]=strtolower(mysql_field_name($this->nResult,$i));
}
function MoveNext
{
($this->EOF)
{
$this->nErr=1;
$this->sErr="MoveNext:已经移到记录集末尾!";
;
}
$this->nOff;
($this->nOff>=$this->nRows)
$this->EOF=true;
}
function MoveTo($Off)
{
(empty($Off))
{
$this->nErr=1;
$this->sErr="MoveTo:必须指定偏移量! ";
;
}
(!$this->nResult)
{
$this->nErr=1;
$this->sErr="MoveTo:请先执行查询:Query";
;
}
$this->nOff=$Off;
}
//得到指定行指定列返回
//如果不指定Off将取得下
//如果不指定nFields将取得该行并已形式返回
function GetValue($nFields=-1,$Off=-1)
{
($this->nResult-1)
{
$this->nErr=1;
$this->sErr="GetValue:请先执行Query!";
;
}
($Off>-1)
{
$this->nOff=$Off;
($this->nOff>=$this->nRows)
{
$this->nErr=1;
$this->sErr="GetValue:所要求偏移量太大无法达到!";
;
}
}
(!@mysql_data_seek($this->nResult,$this->nOff))
{
$this->nErr=1;
$this->sErr="GetValue:请求不存在记录!";
;
}
$aResult=mysql_fetch_row($this->nResult);
(is_($nFields)&&$nFields>-1)
{
($nFileds>$this->nCols)
{
$this->nErr=1;
$this->sErr="GetValue:所请求列值大于实际列值!";
;
}
$aResult[$nFields];
}
(is_($nFields))
{
$nFields=strtolower($nFields);
for($i=0;$i<$this->nCols;$i)
{
($this->aFName[$i]$nFields)
;
}
($i$this->nCols)
{
$this->nErr=1;
$this->sErr="GetValue:所请求列不存在请仔细检查!";
;
}
$aResult[$i];
}
$aResult;
}
function AddNew($TableName="") //标志开始添加数据
{
$this->Initialize;
(!empty($TableName))
$this->sTName=$TableName;
($this->NewEdit>0)
{
$this->nErr=1;
$this->sErr="AddNew:你正在对数据库进行添加或更新操作!";
;
}
(empty($this->sTName))
{
$this->nErr=1;
$this->sErr="AddNew:想要添加数据库表为空可以在构造时指定也可在AddNew时指定!";
;
}
un($this->aNew);
$this->aNew=.gif' />;
$this->NewEdit=1;
$strSQL="select * from ".$this->sTName;
$this->sSQL=$strSQL;
(!$this->nResult=mysql_query($strSQL))
{
$this->nErr=1;
$this->sErr="AddNew:SQL语句:".strSQL."<br><br>MySql:".mysql_error;
;
}
$this->nCols=mysql_num_fields($this->nResult);
un($this->aFName);
$this->aFName=.gif' />;
for($i=0;$i<$this->nCols;$i)
$this->aFName[$i]=strtolower(mysql_field_name($this->nResult,$i));
}
function Edit($Condition="",$TableName="") //对指定数据库表进行编辑
{
$this->Initialize;
(!empty($TableName))
$this->sTName=$TableName;
$this->sEditCon=$Condition;
(empty($this->sTName))
{
$this->nErr=1;
$this->sErr="Edit:在编辑前请先指定数据库表!";
;
}
un($this->aNew);
$this->aNew=.gif' />;
$this->NewEdit=2;
$strSQL="select * from ".$this->sTName;
$this->sSQL=$strSQL;
(!$this->nResult=mysql_query($strSQL))
{
$this->nErr=1;
$this->sErr="Edit:SQL语句:".strSQL."<br><br>MySql:".mysql_error;
;
}
$this->nCols=mysql_num_fields($this->nResult);
un($this->aFName);
$this->aFName=.gif' />;
for($i=0;$i<$this->nCols;$i)
$this->aFName[$i]=strtolower(mysql_field_name($this->nResult,$i));
}
function SetValue($Index,$Value) //指定数据跟在AddNew后执行;
{
($this->NewEdit0)
{
$this->nErr=1;
$this->sErr="SetValue:请先执行AddNew或者Edit!";
;
}
(is_($Index))
{
($Index<0||$Index>$this->nCols)
{
$this->nErr=1;
$this->sErr="SetValue:插入不存在列值!";
;
}
$this->aNew[$Index]=$Value;
$tmpIn=$Index;
}
(is_($Index))
{
$Index=strtolower($Index);
for($i=0;$i<$this->nCols;$i)
{
($this->aFName[$i]$Index)
;
}
($i$this->nCols)
{
$this->nErr=1;
$this->sErr="SetValue:插入不存在列值!";
;
}
$this->aNew[$i]=$Value;
$tmpIn=$i;
}
(!empty($this->sName))
$this->sName.=",";
$this->sName.=$this->aFName[$tmpIn];
//根据当前字段类型生成相应新值
($this->sValue!="#@!")
$this->sValue.=",";

$this->sValue="";
$ftype=@mysql_field_type($this->nResult,$i);
//echo($ftype.",".$this->aNew[$i].",".$i.":".$sValue."<br>");
switch($ftype)
{
"":
"date":
"datetime":
$this->sValue.=""".$this->aNew[$tmpIn].""";
$this->sEdit=""".$this->aNew[$tmpIn].""";
;
"":
"unknown":
$this->sValue.=$this->aNew[$tmpIn];
$this->sEdit=$this->aNew[$tmpIn];
;
default:
$this->nErr=1;
$this->sErr="Update:字段名为".$this->aFName[$tmpIn]."".$ftype."类型目前版本不支持请用别思路方法添加数据!";
;
}
($this->NewEdit2)
$this->sName.="=".$this->sEdit;
}
function Update //存储新值到数据库
{
$strSQL="";
($this->NewEdit0)
{
$this->nErr=1;
$this->sErr="Update:请先执行AddNew或者Edit再用SetValue添加值!";
;
}
(empty($this->sValue))
{
$this->nErr=1;
$this->sErr="Update:在数据为空情况下不能添加或修改数据!";
;
}
switch($this->NewEdit)
{
1: //添加
$strSQL="insert o ";
$strSQL.=$this->sTName;
$strSQL.=" (".$this->sName.") ";
$strSQL.="values (".$this->sValue.")";
;
2: //修改
$strSQL="update ";
$strSQL.=$this->sTName;
$strSQL.=" ";
$strSQL.=$this->sName;
(!empty($this->sEditCon))
$strSQL.=" where ".$this->sEditCon;
;
default:
$this->nErr=1;
$this->sErr="Update:Update生成SQL语句出错请检查!";
;
}
$this->sSQL=$strSQL;
(!$this->nResult=mysql_query($strSQL))
{
$this->nErr=1;
$this->sErr="Update:SQL语句:".$strSQL."<br><br>MySql:".mysql_error;
;
}
//echo($this->sSQL."<br>");
//作清理工作
$this->NewEdit=0;
un($this->aNew);
mysql_query("commit");
}
}
?>
Tags: 

延伸阅读

最新评论

发表评论