php简单操作mysql数据库的类
本文实例讲述了php简单操作mysql数据库的类。分享给大家供大家参考。具体如下:
<?php /** *Databaseclass * *@version:2.2 *@revised:27may2007 * **/ classDatabase{ var$host; var$name; var$user; var$pass; var$prefix; var$linkId; functionDatabase($mysql){ foreach($mysqlas$k=>$v){ $this->$k=$v; } if(strlen($this->prefix)>0&&substr($this->prefix,-1)!=="_") $prefix.="_"; $this->prefix=$prefix; } functiongetLastID(){ $id=mysql_fetch_row(mysql_query("SELECTLAST_INSERT_ID()",$this->linkId)); return$id[0]; } functiongetPossibleValues($tableA,$whereA){ if(gettype($tableA)=="array"){ $table=""; foreach($tableAas$t){ $table.=$this->prefix.$t.","; } $table=substr($table,0,-2); }else$table=$this->prefix.$tableA; if(gettype($whereA)!="array"){ $whereA=array($whereA); } $return=array(); foreach($whereAas$where){ $sql=mysql_query("SHOWCOLUMNSFROM".$table."LIKE'%".$where."%'"); while($arr=mysql_fetch_array($sql)){ if(strpos($arr['Type'],'enum')===0){ $vals=substr($arr['Type'],5,-1); }else{ $vals=substr($arr['Type'],4,-1); } $vals=str_replace("'","",$vals); $vals=explode(",",$vals); $i=1; foreach($valsas$val){ $return[$arr['Field']][$i++]=$val; } $return[$arr['Field']]['default']=$arr['Default']; if($arr['Null']!="NO")$return[$arr['Field']][0]=NULL; } } return$return; } functionconnect(){ $this->linkId=mysql_connect($this->host,$this->user,$this->pass); if(!$this->linkId){ returnfalse; } if(mysql_select_db($this->name,$this->linkId))returntrue; mysql_close($this->linkId); returnfalse; } functionrunSelect($tables,$where="1",$fieldsA="*",$order=false,$limit=false,$offset=false,$group=false){ if(gettype($tables)=="array"){ $table=""; foreach($tablesas$t){ $table.=$this->prefix.$t.","; } $table=substr($table,0,-2); }else$table=$this->prefix.$tables; if(gettype($fieldsA)=="array"){ $fields=""; $keys=array_keys($fieldsA); if($keys[0]!='0'){ foreach($keysas$key){ $fields.=$key.'AS'.$fieldsA[$key].','; } }else{ foreach($fieldsAas$field){ $fields.=$field.','; } } $fields=substr($fields,0,-2); }else$fields=$fieldsA; $query="SELECT".$fields."FROM".$table."WHERE".$where. ($order!==false?"ORDERBY".$order:($group!==false?"GROUPBY".$group:"")). ($limit!==false?"LIMIT".$limit:""). ($offset!==false?"OFFSET".$offset:""); returnmysql_query($query,$this->linkId); } functionrunUpdate($table,$valuesA,$where="1"){ if(gettype($valuesA)=="array"){ $fields=""; $values=""; $keys=array_keys($valuesA); foreach($keysas$key){ if($valuesA[$key]!==NULL) $values.="`".$key."`='".str_replace("'",'\'',$valuesA[$key])."',"; else $values.=$key."=NULL,"; } $fields=substr($fields,0,-1); $values=substr($values,0,-1); }else$values=$valuesA; $query="UPDATE".$this->prefix.$table."SET".$values."WHERE".$where; if(mysql_query($query, $this->linkId)) returnmysql_affected_rows($this->linkId); returnfalse; } functionrunDelete($table,$where="1"){ if(mysql_query("DELETEFROM".$this->prefix.$table."WHERE".$where,$this->linkId)) returnmysql_affected_rows($this->linkId); returnfalse; } functionrunInsert($table,$valuesA,$onDuplicate=NULL){ if(gettype($valuesA)=="array"){ $fields=""; $values=""; $keys=array_keys($valuesA); foreach($keysas$key){ $fields.="`".$key."`,"; $values.=($valuesA[$key]===NULL?"NULL,":"'".str_replace("'",'\'',$valuesA[$key])."',"); } $fields=substr($fields,0,-2); $values=substr($values,0,-2); } $onDup=""; if($onDuplicate!=NULL){ $onDup="ONDUPLICATEKEYUPDATE"; if(gettype($onDuplicate)=="array"){ $keys=array_keys($onDuplicate); foreach($keysas$key){ $onDup.='`'.$key.'`='.($onDuplicate[$key]===NULL?"NULL,":"'".str_replace("'",'\'',$onDuplicate[$key])."',"); } $onDup=substr($onDup,0,-2); }else$onDup.=$onDuplicate; } $query="INSERTINTO".$this->prefix.$table.($fields!==NULL?"(".$fields.")":""). "VALUES(".$values.")".$onDup; if(mysql_query($query,$this->linkId)) returnmysql_affected_rows($this->linkId); returnfalse; } functiongetCells($table){ $query="SHOWCOLUMNSFROM`".$table."`"; $fields=mysql_query($query,$this->linkId)ordie('hej'); return$fields; } functiontranslateCellName($cellName){ $sql=$this->runSelect("mysql_cell_translation","mysql_name='".$cellName."'"); $row=mysql_fetch_assoc($sql); return$row['human_name']?$row['human_name']:'<spanclass="faded">['.$cellName.']</span>'; } functiongetError(){ returnmysql_error($this->linkId); } functionclose() { mysql_close($this->linkId); } } ?>
希望本文所述对大家的php+mysql数据库程序设计有所帮助。