PHP实现的mysql操作类【MySQL与MySQLi方式】
本文实例讲述了PHP实现的mysql操作类。分享给大家供大家参考,具体如下:
首先是mysql方式
ut=$ut;
$this->connect();
}
//数据库的链接
functionconnect(){
$link=mysql_connect($this->host,$this->name,$this->pass)ordie($this->error());
mysql_select_db($this->table,$link)ordie("没该数据库:".$this->table);
mysql_query("SETNAMES'$this->ut'");
}
functionquery($sql,$type=''){
if(!($query=mysql_query($sql)))$this->show('Say:',$sql);
return$query;
}
functionshow($message='',$sql=''){
if(!$sql)echo$message;
elseecho$message.'
'.$sql;
}
functionaffected_rows(){
returnmysql_affected_rows();
}
functionresult($query,$row){
returnmysql_result($query,$row);
}
functionnum_rows($query){
return@mysql_num_rows($query);
}
functionnum_fields($query){
returnmysql_num_fields($query);
}
functionfree_result($query){
returnmysql_free_result($query);
}
functioninsert_id(){
returnmysql_insert_id();
}
functionfetch_row($query){
returnmysql_fetch_row($query);
}
functionversion(){
returnmysql_get_server_info();
}
functionclose(){
returnmysql_close();
}
//向$table表中插入值
functionfn_insert($table,$name,$value){
$this->query("insertinto$table($name)value($value)");
}
//根据$id值删除表$table中的一条记录
functionfn_delete($table,$id,$value){
$this->query("deletefrom$tablewhere$id=$value");
echo"id为".$id."的记录被成功删除!";
}
}
$db=newConnectionMySQL();
$db->fn_insert('test','id,name,sex',"'','hongtenzone','M'");
$db->fn_delete('test','id',1);
?>
mysqli的方式:
每页多少条,0为不分页显示
private$PageCount=1;//分页显示->总共有多少条
private$PageNum=1;//分页显示->总共有多少页
private$PageNo=1;//分页显示->当前第几页
private$PageKey='page';//分页url参数键
private$PageStart=0;//分页显示->当前从第几条开始返回
private$Select;
private$Rest;
private$Result=false;//结果集
public$FormArray=array();
public$Instr_ID=0;
private$j=0;
publicfunctionParameter($Loca,$Root,$Pass,$Base,$Code,$PreFix=''){
$this->LoaclUser=$Root;
$this->LocalBase=$Base;
$this->LocalCode=$Code;
$this->LocalHost=$Loca;
$this->LocalPass=$Pass;
$this->PreFix=$PreFix;
return$this;
}
privatefunctionConnection($Sql){
!function_exists(mysqli_connect)?die('查询失败,无法加载mysqli扩展'):null;
$this->Conn=@newmysqli($this->LocalHost,$this->LoaclUser,$this->LocalPass,$this->LocalBase);
$this->Error=mysqli_connect_errno()==0?true:false;
!$this->Error?die('数据库连接错误,请检查数据库连接参数'):null;
$this->Conn->query('SETNAMES'.$this->LocalCode);
$this->Rest=$this->Conn->query($Sql);
$this->Err=mysqli_error($this->Conn);
$this->Instr_ID=mysqli_insert_id($this->Conn);
$this->Rest->free_result;
$this->Conn->close;
$this->FormArray='';
return$this;
}
publicfunctionnull(){
$this->PageSize=0;
//$this->PageCount=1;
$this->PageStart=1;
$this->Field='*';
$this->Select='';
unset($this->Table,$this->Where,$this->Order,$this->Result);
}
publicfunctionTable($TableName){//数据表
$this->null();
$this->Table='`'.$this->PreFix.$TableName.'`';
return$this;
}
publicfunctionField($Array='*'){//数据字段
!empty($this->Field)?$this->Field='':null;
$Array=explode(',',$Array);
foreach($Arrayas$field){
$this->Field.=!$this->Start?'`'.$field.'`':',`'.$field.'`';
$this->Start++;
}
$this->Start=0;
return$this;
}
publicfunctionWhere($Where){//条件
$this->Where='where'.$Where;
return$this;
}
publicfunctionOrder($Order){//排序
$this->Order='orderby'.$Order;
return$this;
}
publicfunctionpk($key){//分页url参数键
$this->PageKey=$key;
return$this;
}
publicfunctionPage($PageSize){//分页
$this->PageSize=$PageSize;
$this->PageNo=$this->get($this->PageKey);
$this->PageNo=empty($this->PageNo)||!isset($this->PageNo)||!is_numeric($this->PageNo)||$this->PageNo<1?1:$this->PageNo;
return$this;
}
publicfunctionpost($Key,$Filter=true){
return$Filter?strip_tags($_POST[$Key]):$_POST[$Key];
}
publicfunctionget($Key,$Filter=true){
return$Filter?strip_tags($_GET[$Key]):$_GET[$Key];
}
publicfunctionSel(){
$this->Select='Select'.$this->Field.'from'.$this->Table.$this->Where.$this->Order;
$this->Connection($this->Select);
if($this->Rest->num_rows){
while($Rs=$this->Rest->fetch_assoc()){
$this->Result[]=$Rs;
}
}
$DataBase=$this->Result;
returnempty($DataBase)?false:$DataBase;
}
publicfunctionquerys($Sql='',$Type='not',$biao=false){
$this->Select=$Sql;
$this->Connection($this->Select);
if($this->Rest->num_rows){
if(!$biao){
while($Rs=$this->Rest->fetch_array()){
$this->Result[]=!preg_match('/^\d+$/i',$Type)?$Rs:$Rs[$Type];
}
}else{
while($Rs=$this->Rest->fetch_assoc()){
$this->Result[]=$Rs;
}
}
}
$DataBase=$this->Result;
returnempty($DataBase)?false:$DataBase;
}
publicfunctionexecutes($Sql=''){
$this->Connection($Sql);
return$this->Rest;
}
publicfunctionexists($T='',$F='',$W=''){
if(empty($F)){return0;}
$cmd=empty($W)?'Selectsum('.$F.')as`baiyinum`from`'.$this->PreFix.$T.'`':'Selectsum('.$F.')as`baiyinum`from`'.$this->PreFix.$T.'`Where'.$W;
$this->Connection($cmd);
unset($T,$F,$W,$cmd);
$Rel=$this->Rest->fetch_array();
returnround($Rel['baiyinum'],2);
}
publicfunctionExistsTo($Bili=10000,$T='',$F='',$W=''){
if(empty($F)){return0;}
$cmd=empty($W)?'Selectsum('.$F.')as`baiyinum`from`'.$this->PreFix.$T.'`':'Selectsum('.$F.')as`baiyinum`from`'.$this->PreFix.$T.'`Where'.$W;
$this->Connection($cmd);
unset($T,$F,$W,$cmd);
$Rel=$this->Rest->fetch_array();
returnround($Rel['baiyinum']*$Bili);
}
publicfunctionSelect($Type=true,$ListNum=1){//返回记录(数组形式,返回条数)
$this->Select='Select'.$this->Field.'from'.$this->Table.$this->Where.$this->Order;
if(is_numeric($ListNum)){
if($this->PageSize>0){
$this->Connection($this->Select);//执行查询
$this->PageCount=$this->Rest->num_rows;//取得记录总数
$this->PageNum=ceil($this->PageCount/$this->PageSize);//总共有多少页
$this->PageNo=$this->PageNo>$this->PageNum?$this->PageNum:$this->PageNo;
$this->PageStart=($this->PageNo-1)*$this->PageSize;//当前从第几条开始返回
$this->Select.='limit'.$this->PageStart.','.$this->PageSize;//重新构造sql语句
}else{
$this->Select.='limit'.$ListNum;//重新构造sql语句
}
}else{
$this->Select.='limit1';//重新构造sql语句
}
//echo$this->Select;
$this->Connection($this->Select);//再次执行查询
if($this->Rest->num_rows){//如果记录存在
if($Type){
while($Rs=$this->Rest->fetch_array()){
$this->Result[]=$Rs;
}
}else{
while($Rs=$this->Rest->fetch_assoc()){
$this->Result[]=$Rs;
}
}
}
if(($ListNum==1or!is_numeric($ListNum))&&!$this->PageSize){$this->Result=$this->Result[0];}
$DataBase=$this->Result;
returnempty($DataBase)?false:$DataBase;
}
publicfunctionNum(){//返回记录总数
$this->Select='Select'.$this->Field.'from'.$this->Table.$this->Where.$this->Order;
$this->Connection($this->Select);//执行查询
return$this->Rest->num_rows;//取得记录总数
}
publicfunctionPageNav($NumNav=false){//分页
$Action=$this->get('action');
!empty($Action)or$Action='index';
$Module=$this->get('module');
!empty($Module)or$Module='index';
$NavUrl='/'.$Module.'/'.$Action.'/'.$this->PageKey.'/';
$NaIndex='/'.$Module.'/'.$Action;
$PageHtml="\n";
$PageHtml.=''.$this->PageCount.'条记录'.$this->PageNo.'/'.$this->PageNum.'页';
$this->PageNo<=1or$PageHtml.="首页\nPageNo-1)."\">上一页\n";
if($NumNav){$PageHtml.=$this->NumPage($NavUrl);}
$this->PageNo>=$this->PageNumor$PageHtml.="PageNo+1)."\">下一页\nPageNum."\">尾页\n";
$PageHtml.="