mysql仿asp的数据库操作类
code:[复制到剪切板]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);
}
}
?>