PHP数据库处理封装类实例
本文实例讲述了PHP数据库处理封装类。分享给大家供大家参考,具体如下:
MySQL的操作相关类,检查并使用了mysqli
<?php //sample15_12.php classmydb{ private$user; private$pass; private$host; private$db; //Constructorfunction. publicfunction__construct(){ $num_args=func_num_args(); if($num_args>0){ $args=func_get_args(); $this->host=$args[0]; $this->user=$args[1]; $this->pass=$args[2]; $this->connect(); } } //Functiontotellusifmysqliisinstalled. privatefunctionmysqliinstalled(){ if(function_exists("mysqli_connect")){ returntrue; }else{ returnfalse; } } //Functiontoconnecttothedatabase. privatefunctionconnect(){ try{ //Mysqlifunctionality. if($this->mysqliinstalled()){ if(!$this->db=newmysqli($this->host,$this->user,$this->pass)){ $exceptionstring="Errorconnectiontodatabase:<br/>"; $exceptionstring.=mysqli_connect_errno().":".mysqli_connect_error(); thrownewexception($exceptionstring); } //Mysqlfunctionality. }else{ if(!$this->db=mysql_connect($this->host,$this->user,$this->pass)){ $exceptionstring="Errorconnectiontodatabase:<br/>"; $exceptionstring.=mysql_errno().":".mysql_error(); thrownewexception($exceptionstring); } } }catch(exception$e){ echo$e->getmessage(); } } //Functiontoselectadatabase. publicfunctionselectdb($thedb){ try{ //Mysqlifunctionality. if($this->mysqliinstalled()){ if(!$this->db->select_db($thedb)){ $exceptionstring="Erroropeningdatabase:$thedb:<br/>"; $exceptionstring.=$this->db->errno.":".$this->db->error; thrownewexception($exceptionstring); } //Mysqlfunctionality. }else{ if(!mysql_select_db($thedb,$this->db)){ $exceptionstring="Erroropeningdatabase:$thedb:<br/>"; $exceptionstring.=mysql_errno().":".mysql_error(); thrownewexception($exceptionstring); } } }catch(exception$e){ echo$e->getmessage(); } } //Functiontoperformaquery. publicfunctionexecute($thequery){ try{ //Mysqlifunctionality. if($this->mysqliinstalled()){ if(!$this->db->query($thequery)){ $exceptionstring="Errorperformingquery:$thequery:<br/>"; $exceptionstring.=$this->db->errno.":".$this->db->error; thrownewexception($exceptionstring); }else{ echo"Queryperformedcorrectly:".$this->db->affected_rows."row(s)affected.<br/>"; } //Mysqlfunctionality. }else{ if(!mysql_query($thequery,$this->db)){ $exceptionstring="Errorperformingquery:$thequery:<br/>"; $exceptionstring.=mysql_errno().":".mysql_error(); thrownewexception($exceptionstring); }else{ echo"Queryperformedcorrectly:".mysql_affected_rows()."row(s)affected.<br/>"; } } }catch(exception$e){ echo$e->getmessage(); } } //Functiontoreturnarowset. publicfunctiongetrows($thequery){ try{ //Mysqlifunctionality. if($this->mysqliinstalled()){ if($result=$this->db->query($thequery)){ $returnarr=array(); while($adata=$result->fetch_array()){ $returnarr=array_merge($returnarr,$adata); } return$returnarr; }else{ $exceptionstring="Errorperformingquery:$thequery:<br/>"; $exceptionstring.=$this->db->errno.":".$this->db->error; thrownewexception($exceptionstring); } //Mysqlfunctionality. }else{ if(!$aquery=mysql_query($thequery)){ $exceptionstring="Errorperformingquery:$thequery:<br/>"; $exceptionstring.=mysql_errno().":".mysql_error(); thrownewexception($exceptionstring); }else{ $returnarr=array(); while($adata=mysql_fetch_array($aquery)){ $returnarr=array_merge($returnarr,$adata); } return$returnarr; } } }catch(exception$e){ echo$e->getmessage(); } } //Functiontoclosethedatabaselink. publicfunction__destruct(){ try{ //Mysqlifunctionality. if($this->mysqliinstalled()){ if(!$this->db->close()){ $exceptionstring="Errorclosingconnection:<br/>"; $exceptionstring.=$this->db->errno.":".$this->db->error; thrownewexception($exceptionstring); } //Mysqlfunctionality. }else{ if(!mysql_close($this->db)){ $exceptionstring="Errorclosingconnection:<br/>"; $exceptionstring.=mysql_errno().":".mysql_error(); thrownewexception($exceptionstring); } } }catch(exception$e){ echo$e->getmessage(); } } } //Now,letuscreateaninstanceofmydb. $mydb=newmydb("localhost","root",""); //Selectadatabasetouse. $mydb->selectdb("wps"); //Now,let'sperformanaction. //$adata=$mydb->execute("UPDATEcdSETtitle='HybridTheory'WHEREcdid='2'"); //Then,let'strytoreturnarowset. $adata=$mydb->getrows("SELECT*FROMwp_terms"); for($i=0;$i<count($adata);$i++){ echo$adata[$i]."<br/>"; } $mydb->selectdb("test"); $result=$mydb->execute("UPDATEuserSETage=23WHEREid=2"); echo"<br/>"; echo$result; ?>
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php+mysql数据库操作入门教程》、《PHP基于pdo操作数据库技巧总结》、《PHP+MongoDB数据库操作技巧大全》、《php+Oracle数据库程序设计技巧总结》、《php+mssql数据库程序设计技巧总结》、《php+redis数据库程序设计技巧总结》、《php+mysqli数据库程序设计技巧总结》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。