错误号以及封装类!!!!

 错误号::::
  ![在这里插入图片描述](https://img-blog.csdnimg.cn/20181123201919913.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzc4MTE1NA==,size_16,color_FFFFFF,t_70)

自己学习中封装的数据库类

<?php //封装数据库类 class db{ //数据库服务器 protected $host = 'localhost'; //数据库用户名 protected $user = 'root'; //数据库密码 protected $pwd = ''; //端口号 protected $port = 3306; //数据库名 protected $db; //表名 protected $table; //字符集 protected $charset='utf8'; //表前缀 protected $prev; //连接信息 protected $link; public function __construct(array $arr){ //属性赋值 $this->option($arr); //给连接信息赋值 $this->link = $this->connect(); } //准备sql语句 insert/delete/update/select 表名 //给表名赋值 public function table($table){ if(!empty($table)){ $this->table = $table; }else{ return false; } } //增 public function insert(array $arr){ $str = ""; foreach($arr as $k=>$v){ $str .= "$k='$v',"; } $str = rtrim($str,","); $table = $this->table; $sql = "insert into $table set $str"; //var_dump($sql); return $this->query($sql); } //删 public function del($where){ if(empty($where)){ return false; die; } $table=$this->table; $sql = "delete from $table where $where"; return $this->query($sql); } //改 public function update(array $files,$where){ $str=''; foreach($files as $k=>$v){ $str .= "$k='$v',"; } $str = rtrim($str,","); $table = $this->table; $sql ="update $table set $str where $where"; return $this->query($sql); } //查 public function select($where="",$files="*"){' $table =$this->table; $sql = "select $files from $table where $where"; if(empty($where)){ $sql = "select $files from $table"; } return $this->query($sql); } //发送sql语句 protected function query($sql){ $res = mysqli_query($this->link,$sql); if(is_object($res)){ while($date = mysqli_fetch_assoc($res)){ $arr[] = $date; } return $arr; } //var_dump($res);die; return $res; } //连接数据库 protected function connect(){ $link = mysqli_connect($this->host,$this->user,$this->pwd,$this->db); if(!$link){ echo "数据库连接失败"; } mysqli_set_charset($link,$this->charset); return $link; } //给属性一一赋值 protected function option(array $arr){ foreach($arr as $k=>$v){ $this->$k = $v; } } } ?>

还有自己封装的分页类:

<?php //分页类 class page{ //偏移量 public $offset; //每页显示数 public $num; //当前页 protected $page; //总页数 protected $CountPage; //上一页 protected $prev; //下一页 protected $next; //尾页 protected $end; //首页 protected $first; //总条数 protected $count; //url protected $url; //构造方法 给各个属性赋值 总条数 每页显示数 public function __construct($count,$num=5){ //给各个属性赋值 $this->count = $count>0?$count:1; $this->num = $num; $this->page = $this->getpage(); $this->CountPage = $this->getCountPage(); $this->offset = $this->getoffset(); $this->prev = $this->getPrev(); $this->next = $this->getNext(); $this->first = $this->getFirst(); $this->end = $this->getEnd(); $this->url = $this->getUrl(); } //获取url protected function getUrl(){ //url的构成 http://主机名 端口号 地址 ?传参 $REQUEST_SCHEME = $_SERVER['REQUEST_SCHEME']; $ServerName = $_SERVER['SERVER_NAME']; $port = $_SERVER['SERVER_PORT']; $PHP_SELF = $_SERVER['PHP_SELF']; if(!empty($_SERVER['QUERY_STRING'])){ $string = $_SERVER['QUERY_STRING']; parse_str($string,$arr); unset($arr['page']); $PHP_SELF .= "?" . http_build_query($arr); } $url = $REQUEST_SCHEME . "://" . $ServerName . ":" . $port . $PHP_SELF ; return $url; } //公共的类外调用的方法 public function diaoyong(){ $arr = array( 'prev' => $this->setprev(), 'next' => $this->setnext(), 'first' => $this->setfirst(), 'end' => $this->setend() ); return $arr; } //上一页跳转地址、 protected function setprev(){ if(substr_count($this->url,"?")){ $url = $this->url . "&page=" . $this->prev; }else{ $url = $this->url . "?page=" . $this->prev; } return $url; } //下一页跳转地址、 protected function setnext(){ if(substr_count($this->url,"?")){ $url = $this->url . "&page=" . $this->next; }else{ $url = $this->url . "?page=" . $this->next; } return $url; } //shou页跳转地址、 protected function setfirst(){ if(substr_count($this->url,"?")){ $url = $this->url . "&page=" . $this->first; }else{ $url = $this->url . "?page=" . $this->first; } return $url; } //尾页跳转地址、 protected function setend(){ if(substr_count($this->url,"?")){ $url = $this->url . "&page=" . $this->end; }else{ $url = $this->url . "?page=" . $this->end; } return $url; } //求上一页 protected function getPrev(){ $prev = $this->page > 1 ? $this->page -1 : 1; return $prev; } //求下一页 protected function getNext(){ $next = $this->page < $this->CountPage ? $this->page + 1 : $this->CountPage; return $next; } //求首页 protected function getFirst(){ $frist = 1; return $frist; } //求尾页 protected function getEnd(){ $end = $this->CountPage; return $end; } //求总页数 protected function getCountPage(){ $CountPage = ceil($this->count / $this->num); return $CountPage; } //求取当前页 protected function getpage(){ //如果get 没有传过来page 当前页就是首页 $page = empty($_GET['page'])?1:$_GET['page']; return $page; } //求取偏移量 protected function getoffset(){ $offset = ($this->page - 1) * $this->num; return $offset; } } ?>

上传类 :

<?php /* 文件上传 1.from表单 2.判断错误号 3.判断文件大小 4.判断文件MIME类型 及文件后缀 5.生成新路径,新的文件名 挪动文件到新路径里 6.判断是否上传成功 7.将新路径加文件名保存到数据库 */ class upload{ //新路径 protected $Path = './upload/'; //上传的文件名 protected $OrgName; //上传文件的临时名字 protected $TmpName; //上传文件的新名字 protected $NewName; //是否启用随机名 protected $isRandName = true; //文件类型 protected $Type; //文件后缀 protected $SubFix; //文件前缀 protected $PreFix; //错误号 protected $Error; //错误信息 protected $ErrorInfo; //文件大小 protected $Size; //准许的文件大小 protected $AllowSize = 2000000; //准许的文件类型 protected $AllowType = array('image/png' , 'image/jpeg' , 'image/jpg' , 'image/gif' , 'image/wbmp'); //准许的文件后缀 protected $AllowSub = array('png' , 'jpeg' , 'jpg' , 'gif' , 'wbmp'); //构造方法 function __construct(array $arr = array()){ //给属性一一赋值 foreach($arr as $k=>$v){ //在类外 获取当前类公有属性 在类内获取所有属性 if(!array_key_exists($k,get_class_vars(get_class($this)))){ continue; } $this->Option($k,$v); } } //公共的上传的方法 $name 是 input框利的name public function up($name){ var_dump($_FILES["$name"]); //获得错误号 $this->Error = $_FILES["$name"]['error']; $this->ErrorInfo = $this->getErrorInfo(); $this->OrgName = $_FILES["$name"]['name']; $this->Type = $_FILES["$name"]['type']; $this->TmpName = $_FILES["$name"]['tmp_name']; $this->Size = $_FILES["$name"]['size']; //判断是否有错误 if(empty($this->ErrorInfo)){ //判断大小 if($this->Size > $this->AllowSize){ $this->Error = -1; return false; } //判断文件类型和后缀 if(in_array($)) }else{ return false; } } //获取错误信息 protected function getErrorInfo(){ $str=""; switch($this->error){ case -1: $str = "文件大小超过了,我准许的大小"; break; case 1: $str = '其值为 1,上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值'; break; case 2: $str = '其值为 2,上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。'; break; case 3: $str = '其值为 3,文件只有部分被上传。 '; break; case 4: $str = '其值为 4,没有文件被上传。 '; break; case 6: $str = '其值为 6,找不到临时文件夹。PHP 4.3.10 和 PHP 5.0.3 引进。'; break; case 7: $str = '其值为 7,文件写入失败。PHP 5.1.0 引进。 '; break; } return $str; } protected function Option($k,$v){ $this->$k = $v; } } ?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值