对mysql的数据库操作类_MYSQL数据库操作类

class MSSQLDB

{

//MYSQL数据库操作类

//使用说明:

//该类完全按照ADO的习惯书写的,用过ASP的人都觉得ASP连接数据库比PHP好用(这是我的感觉),

//但PHP得一个一个API地写,挺累,该类做了完全的封装

//创建类的实例时可以指定一个数据库表和选择的数据库,如:new MySQLDB("table","database");

//查询数据时Query后可以用GetValue得到相应的值,既可以是字段名也可以是已0开始的序号

//插入新值,先用AddNew后使用SetValue相应的字段名或序号和字段值,在用Update添加

//编辑时用Edit指定编辑记录的条件在使用SetValue,最后用Update添加

//在类使用过程中,sTName记录上次使用的数据库表名,当指定后可以直接使用,以后的操作默认在该表

//上进行操作,当然也可以每次指定特殊的表进行操作

//nErr指示是否操作出错,sErr记录最后一次出错的错误代码,记录了明确的有哪个函数引起的错误

var $host='localhost';       //主机名

var $user='root';              //用户名

var $password='0';          //用户密码

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="#@!";

//$this->sValue='';

//$this->sEdit='';

unset($this->aFName);

unset($this->aNew);

}

function MSSqlDB($TableName="",$database="rck",$db_host="localhost",$db_user='sa',$db_password='sa')  //构造函数

{

$this->host=$db_host;

$this->user=$db_user;

$this->password=$db_password;

$this->Initialize();

$this->sTName=$TableName;

$this->linkid=mssql_connect($this->host,$this->user,$this->password);

if(!$this->linkid)

{

$this->nErr=1;

$this->sErr="MySqlDB:数据库连接出错,请启动服务!";

return;

}

$this->dbid=mssql_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()          //数据清除处理

{

mssql_query("commit");

mssql_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;

$result=mssql_query($SQL,$this->linkid);

if(!$result)

{

$this->nErr=1;

$this->sErr="Execute:SQL语句:".$SQL."

MySql错误:".mssql_error();

return false;

}

return $result;

}

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=mssql_query($strSQL))

{

$this->nErr=1;

$this->sErr="Query:SQL语句:".$strSQL."

MySql错误:".mssql_error()."

";

return;

}

$this->nOffset=0;

$this->nRows=mssql_num_rows($this->nResult);

$this->nCols=mssql_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(mssql_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(mailto:!@mssql_data_seek($this-%3EnResult,$this-%3EnOffset))

{            $this->nErr=1;

$this->sErr="GetValue:请求不存在的记录!";

return;

}

$aResult=mssql_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=mssql_query($strSQL))

{

$this->nErr=1;

$this->sErr="AddNew:SQL语句:".strSQL."

MSSql错误:".mssql_error();

return;

}

$this->nCols=mssql_num_fields($this->nResult);

unset($this->aFName);

$this->aFName=array();

for($i=0;$inCols;$i++)

$this->aFName[$i]=strtolower(mssql_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=mssql_query($strSQL))

{

$this->nErr=1;

$this->sErr="Edit:SQL语句:".strSQL."

MySql错误:".mssql_error();

return;

}

$this->nCols=mssql_num_fields($this->nResult);

unset($this->aFName);

$this->aFName=array();

for($i=0;$inCols;$i++)

$this->aFName[$i]=strtolower(mssql_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;$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;

}

switch($this->NewEdit){

case 1:

if(!empty($this->sName)){

$this->sName.=",";

}

$this->sName.=$this->aFName[$tmpIn];

if($this->sValue=='#@!'){

$this->sValue="'".$Value."'";

}else{

$this->sValue.=","."'".$this->aNew[$tmpIn]."'";

}

break;

case 2:

if($this->sEdit=='#@!'){

$this->sEdit='';

$this->sEdit.="$Index"."="."'".$Value."'";

}else{

$this->sEdit.=","."$Index"."="."'".$Value."'";

}

break;

}

}

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->sEdit;

if(!empty($this->sEditCon))

$strSQL.=" where ".$this->sEditCon;

break;

default:

$this->nErr=1;

$this->sErr="Update:Update()生成SQL语句出错,请检查!";

return;

}

$strSQL.=" ; ";

$this->sSQL=$strSQL;

if(!$this->nResult=mssql_query($strSQL))

{

$this->nErr=1;

$this->sErr="Update:SQL语句:".$strSQL."

MySql错误:".mssql_error();

return;

}

//作清理工作

$this->NewEdit=0;

unset($this->aNew);

}

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值