function MySqlDB($TableName="",$database="mysql") //构造函数
{
$this->nErr=0;
$this->NewEdit=0;
$this->sTName=$TableName;
$this->nResult=-1;
$this->nCols=0;
$this->nRows=0;
$this->nOffset=0;
$this->EOF=true;
unset($this->aFName);
unset($this->aNew);
if(!$this->linkid=mysql_connect($host,$user,$password))
{
$this->nErr=1;
$this->sErr="MySqlDB:数据库连接出错,请启动服务!";
return;
}
if(!$this->dbid=mysql_select_db($database))
{
$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;
}
//在数据库里执行查询,SQL是查询的字段,Condition是查询条件,Order是以什么排序,Sequenc是按升序还是降序
function Query($TableName="",$SQL="*",$Condition="",$Order="",$Sequenc="")
{
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="") //标志开始添加数据
{
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="") //对指定数据库表进行编辑
{
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;
return;
}
if(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;
return;
}
}
function Update() //存储新值到数据库
{
$strSQL="";
$sName="";
$sValue="#@!";
$sEdit="#@!";
if($this->NewEdit==0)
{
$this->nErr=1;
$this->sErr="Update:请先执行AddNew()或者Edit(),再用SetValue()添加值!";
return;
}
//得到想要添加的字段名
for($i=0;$i<$this->nCols;$i++)
{
if(is_int($this->aNew[$i])||(is_string($this->aNew[$i])&&!empty($this->aNew[$i])))
{
if(!empty($sName))
$sName.=",";
$sName.=$this->aFName[$i];
//根据当前字段的类型生成相应的新值
if($sValue!="#@!")
$sValue.=",";
else
$sValue="";
$ftype=@mysql_field_type($this->nResult,$i);
//echo($ftype.",".$this->aNew[$i].",".$i.":".$sValue."<br>");
switch($ftype)
{
case "string":
case "date":
$sValue.="\"".$this->aNew[$i]."\"";
$sEdit="\"".$this->aNew[$i]."\"";
break;
case "int":
case "unknown":
$sValue.=$this->aNew[$i];
$sEdit=$this->aNew[$i];
break;
default:
$this->nErr=1; $this->sErr="Update:字段名为".$this->aFName[$i]."的".$ftype."类型目前版本不支持,请用别的方法添加数据!";
return;
}
if($this->NewEdit==2)
$sName.="=".$sEdit;
}
}
if(empty($sValue))
{
$this->nErr=1;
$this->sErr="Update:在数据为空的情况下,不能添加或修改数据!";
return;
}
switch($this->NewEdit)
{
case 1:
$strSQL="insert into ";
$strSQL.=$this->sTName;
$strSQL.=" (".$sName.") ";
$strSQL.="values (".$sValue.")";
break;
case 2:
$strSQL="update ";
$strSQL.=$this->sTName;
$strSQL.=" set ";
$strSQL.=$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;
}
//作清理工作
$this->NewEdit=0;
unset($this->aNew);
mysql_query("commit");
return;
}
}
?>
(出处:www.mesky.net )