简介:这个主要是分享一下给还在研究原生php的MYSQL操作方法封装的朋友。为什么我们要把查询语句封装起来呢?一切从简, 说白了就是懒,少干点事。其实这样开发真的很高效,但需要团队开发的话就需要一份详细的文档了。下面的这个类里封装了包括 增、删、查、改、执行原生sql语句、上传文件,批量上传文件、分页等基本的方法。 <?php class lu_page extends lu_db { private $showPage; private $sPage; private $pageoffset; function __construct( $table,$showPage=15 ) { $this->table = $table; $this->showPage = $showPage; } //连接数据库 public function connect() { $this->conn = new mysqli($this->DBHOST, $this->DBUSER, $this->DBPWD, $this->DBNAME); } //执行查询原生sql语句 public function selectsql($sql){ $this->connect(); $res = $this->conn->query($sql); $row = $res->fetch_all(); return $row; } //执行增加、修改、删除原生sql语句 public function aed($sql){ $this->connect(); $res = $this->conn->multi_query($sql); if($res){ echo "<script>alert('操作成功');</script>"; }else{ echo "<script>alert('操作失败');</script>"; } } //插入一条记录 public function insert($key,$file,$path=null) { $this->connect(); $sql = "insert into ".$this->table."("; if(!empty($key) || $key!=null){ $i = 0; foreach($key as $item=>$value){ $tempItem[$i] = $item; $tempValue[$i] = $value; $i++; } foreach($tempItem as $index=>$value){ if($index == "0"){ $sql1 = $value; }else{ $sql1 .= ",".$value; } } foreach($tempValue as $index=>$value){ if($index == "0"){ $sql2 = "'{$value}'"; }else{ $sql2 .= ",'{$value}'"; } } } $sql3 =""; $sql4 =""; if(!empty($file) || $file!=null){ foreach($file as $item=>$value){ $sql3 .= ",".$item; $imgUrl = $this->uploadFile($value,$path); $sql4 .= ",'{$imgUrl}'"; } } $sql .=$sql1.$sql3.") values(".$sql2.$sql4.")"; $res = $this->conn->multi_query($sql); if($res){ echo "<script>alert('提交成功');</script>"; }else{ echo "<script>alert('提交失败');</script>"; } } //更新一条记录 public function update($key,$where,$file = null,$path=null){ $this->connect(); $sql = "UPDATE ".$this->table." SET "; if(is_array($key)){ $i = 0; foreach($key as $item=>$value){ if($i == 0) { $sql .= $item . "='" .$value."'"; }else{ $sql .= ",".$item . "='" .$value."'"; } $i++; } }else{ $sql .= "".$key.""; } if(!empty($file) || $file!=null){ foreach($file as $item=>$value){ $imgUrl = $this->uploadFile($value,$path); $sql .= ",".$item."='".$imgUrl."'"; } } $sql .= " where ".$where; $res = $this->conn->multi_query($sql); if($res){ echo "<script>alert('提交成功');</script>"; }else{ echo "<script>alert('提交失败');</script>"; } return $message; } /* 查询数据库 key为需要查询的值 page为当前页数 showPage一页展示多少 how如何排序*/ private function select($key,$name,$type) { $sql = "select "; if(is_array($key)){ foreach($key as $index=>$value){ if($index == 0){ $sql .= "$value"; }else{ $sql .= ","."$value"; } } }else{ $sql .= "".$key.""; } $sql .= " from ".$this->table; if(!empty($name) || $name!=null){ $sql .= " where "; foreach($name as $item=>$value){ if($value == "or" || $value == "and"){ $sql .= " ".$value." "; }else { if($type == "like"){ $sql .= $item . " ".$type." " . "'%".$value."%'"; }else{ $sql .= $item . " ".$type." " . "'".$value."'"; } } } } return $sql; } //分页 public function page($key,$name,$page=null,$how=null,$type=null) { $this->connect(); $sql = $this->select($key,$name,$type); $count = $this->select("count(*)",$name,$type); if($how != null){ $sql .= " ".$how.""; } $page ? $page: $page=1; $first = ($page-1)*$this->showPage; $sql .= " limit $first,$this->showPage"; $res = $this->conn->query($sql); $num = $this->conn->query($count); $row = $res->fetch_all(); $count = $num->fetch_assoc(); $this->sPage = ceil($count["count(*)"]/$this->showPage); return $row; } //获取页码 public function pageNumber($page){ $page_banner="<div style='text-align:center'>"; $this->pageoffset=($this->showPage-1)/2; if($page>1){ $page_banner.="<a style='border:#333 1px solid; border-radius:4px; text-decoration:none; padding:2px 5px 2px 5px; margin:2px;' href='".$_SERVER['PHP_SELF']."?p=1'>首页</a>"; $page_banner.="<a style='border:#333 1px solid; text-decoration:none; padding:2px 5px 2px 5px; margin:2px;' href='".$_SERVER['PHP_SELF']."?p=".($page-1)."'>上一页</a>"; }else{ $page_banner.="<span style='border:#eee 1px solid; border-radius:4px; padding:2px 5px 2px 5px; margin:2px; color:#999;'>首页</span>"; $page_banner.="<span style='border:#eee 1px solid; padding:2px 5px 2px 5px; margin:2px; color:#999;'>上一页</span>"; } $start=1; $end=$this->sPage; if($this->sPage > $this->showPage){ if($page > ($this->pageoffset + 1)){ $page_banner.="..."; } if($page > $this->pageoffset){ $start=$page - $this->pageoffset; $end=$this->sPage > $page+$this->pageoffset ? $page+$this->pageoffset:$this->sPage; }else{ $start=1; $end=$this->sPage > $this->showPage ? $this->showPage : $this->sPage; } if($page + $this->pageoffset > $this->sPage){ $start=$start - ($page + $this->pageoffset - $end); } } for($i=$start;$i<=$end;$i++){ if($page==$i){ $page_banner.="<span style='border:#333 1px solid; background-color:#333; padding:3px 6px 3px 6px; margin:2px; color:#fff; font-weight:bold;'>{$i}</span>"; }else{ $page_banner.="<a style='border:#333 1px solid; text-decoration:none; padding:2px 5px 2px 5px; margin:2px;' href='".$_SERVER['PHP_SELF']."?p=".$i."'>{$i}</a>"; } } if($this->sPage > $this->showPage&&$this->sPage > $page + $this->pageoffset){ $page_banner.="..."; } if($page < $this->sPage){ $page_banner.="<a style='border:#333 1px solid; text-decoration:none; padding:2px 5px 2px 5px; margin:2px;' href='".$_SERVER['PHP_SELF']."?p=".($page+1)."'>下一页</a>"; $page_banner.="<a style='border:#333 1px solid; border-radius:4px; text-decoration:none; padding:2px 5px 2px 5px; margin:2px;' href='".$_SERVER['PHP_SELF']."?p=".($this->sPage)."'>尾页</a>"; }else{ $page_banner.="<span style='border:#eee 1px solid; border-radius:4px; padding:2px 5px 2px 5px; margin:2px; color:#999;'>尾页</a></span>"; $page_banner.="<span style='border:#eee 1px solid; padding:2px 5px 2px 5px; margin:2px; color:#999;'>下一页</a></span>"; } $page_banner.="共 {$this->sPage} 页."; /*$page_banner.="<form style='display:inline;' action='kecheng.php' method='get'>"; $page_banner.="到第<input type='text' name='p'>页"; $page_banner.="<button type='submit' value='确定'>确定</button>"; $page_banner.="</form></div>";*/ return $page_banner; } //检查上传文件信息并返回文件名 public function uploadFile($fileInfo,$path,$allowExt=array("png","jpg","mp4","fla","swf","gif","psd","doc","txt"),$maxSize=1512000,$imgFlag=true){ //判断下错误信息 if($path == null){ $path = "../upload"; } if($fileInfo['error']==0){ $ext=$this->getExt($fileInfo['name']); //限制上传文件类型 if(!in_array($ext,$allowExt)){ exit ("非法文件类型"); } if($fileInfo['size']>$maxSize){ exit ("文件过大"); } $filename=$this->getUniName().".".$ext; if(!file_exists($path)){ mkdir($path,0777,true); } $destination=$path."/".$filename; if(is_uploaded_file($fileInfo['tmp_name'])){ if(move_uploaded_file($fileInfo['tmp_name'], $destination)){ }else{ $mes="文件移动失败"; echo "<script>alert('上传失败');</script>"; } }else{ $mes="文件不是通过HTTP POST方式上传上来的"; die($mes); } }else{ switch($fileInfo['error']){ case 1: echo "<script>alert('超过了配置文件上传文件的大小');</script>";//UPLOAD_ERR_INI_SIZE break; case 2: echo "<script>alert('超过了表单设置上传文件的大小');</script>";//UPLOAD_ERR_FORM_SIZE break; case 3: echo "<script>alert('部分文件被上传');</script>";//UPLOAD_ERR_PARTIAL break; case 4: echo "<script>alert('没有文件被上传');</script>";//UPLOAD_ERR_NO_FILE break; case 6: echo "<script>alert('没有找到临时目录');</script>";//UPLOAD_ERR_NO_TMP_DIR break; case 7: echo "<script>alert('文件不可写');</script>";//UPLOAD_ERR_CANT_WRITE; break; case 8: echo "<script>alert('PHP的扩展程序中断了上传');</script>";//UPLOAD_ERR_EXTENSION break; } die($mes); } return $destination; } /** * 生成唯一字符串 * @return string */ private function getUniName(){ return md5(uniqid(microtime(true),true)); } /** * 得到文件的扩展名 * @param string $filename * @return string */ private function getExt($filename){ return strtolower(end(explode(".",$filename))); } }
转载于:https://my.oschina.net/luqiuren666/blog/860208