<?php/** * */class Db{ private $host = ''; private $port = ''; private $user = ''; private $pass = ''; private $dbname = ''; private $charset=''; private $tbp = ''; private $db = ''; /** * 返回 pdo对象 */ public function getDb(){ return $this->db; } /** * @param $tbp 表名前辍 */ public function setTbp($tbp=''){ $this->tbp = $tbp; } public function __construct($host='localhost',$port='3306',$user='root',$pass='',$dbname='fzdb',$charset='utf8',$tbp='fz_'){ $dsn = "mysql:host=$host;port=$port;dbname=$dbname"; $opt = [1002 =>"set names $charset"]; try { $this->db = new PDO ( $dsn, 'root', '', $opt ); $this->host = $host; $this->port = $port; $this->user = $user; $this->pass = $pass; $this->dbname = $dbname; $this->tbp = $tbp; $this->charset = $charset; } catch ( Exception $e ) { exit ( '数据库服务器连接失败,程序终止' ); } } /** * * @param unknown $t 表名,不含执行时会自动加上表前辍 * @param unknown $d 要插入的数据是关联数组 */ public function gomsg($url='',$msg=''){ echo '<script>'; echo "alert('$msg');"; echo "location.href='$url'"; echo '</script>'; } public function save($t,$d){ $t = $this->tbp.$t; foreach($d as $key=>$v){ $kk[] = $key; $vv[] = ':'.$key; } $kk = implode(',',$kk); $vv = implode(',',$vv); $stmt = $this->db->prepare("insert into $t($kk) values($vv)"); $stmt->execute($d); $stmt->closeCursor(); } public function query($t,$f='*',$w='1=1',$o='',$l='limit 10'){ $t = $this->tbp.$t; $stmt = $this->db->prepare("select $f from $t where $w $o $l"); $stmt->execute(); $rs = $stmt->fetchAll(PDO::FETCH_ASSOC); $stmt->closeCursor(); return $rs; } public function delete($t,$w='1=1'){ $t = $this->tbp.$t; $stmt = $this->db->prepare("delete from $t where $w"); $stmt->execute(); $stmt->closeCursor(); } public function getcount($t,$w='1=1'){ $t = $this->tbp.$t; $stmt = $this->db->prepare("select count(*) from $t where $w"); $stmt->execute(); $stmt->bindColumn(1, $c); $stmt->fetchAll(PDO::FETCH_NUM); return $c; } public function updatenum($t,$f,$w='1=1'){ $t = $this->tbp.$t; $stmt = $this->db->prepare("update $t set $f where $w"); $stmt->execute(); $stmt->closeCursor(); } public function pager($t,$currpage=1,$f='*',$pagesize=3,$w='1=1',$o='',$ty=''){ $recordcount = $this->getcount($t,$w); $t = $this->tbp.$t; $stmt = $this->db->prepare("select $f from $t where $w $o limit ?,?"); $pagecount = ceil($recordcount/$pagesize); $currpage = $currpage<1 ? 1 : $currpage; $currpage = $currpage>$pagecount ? $pagecount : $currpage; $start = $currpage*$pagesize - $pagesize; $stmt->bindParam(1, $start, PDO::PARAM_INT); $stmt->bindParam(2, $pagesize, PDO::PARAM_INT); $stmt->execute(); $row[] = $stmt->fetchAll(PDO::FETCH_ASSOC); $first = 1; $end = 10; $pages = '<div class="page">'; if($currpage>=7){ $first = $currpage-5; $end = $first+$end-1; } if($currpage>1){ $prev = $currpage-1; if($first>1){ $pages.="<a href=?".$ty."p=1>首页</a><a href=?".$ty."p=$prev>上一页</a>"; }else{ $pages.="<a href=?".$ty."p=$prev>上一页</a>"; } } for($i=$first;$i<=$end;$i++){ if($i>$pagecount){ break; } if($i==$currpage){ $pages.='<a class="checked">'.$i.'</a>'; continue; } $pages.="<a href=?".$ty."p=$i>$i</a>"; } if($currpage<$pagecount){ $next = $currpage+1; $pages.="<a href=?".$ty."p=$next>下一页</a>"; } if($end<$pagecount){ $pages.="<a href=?".$ty."p=$pagecount>尾页</a>"; } $row[] = $pages.'</div>'; $row[] = $pagesize; $row[] = $pagecount; $row[] = $recordcount; $row[] = $currpage; return $row; } public function css1(){ $css = <<<css <style> .page{font-size:12px;height:30px;padding:15px 0;clear:both;overflow:hidden;text-align:center;} .page a{text-decoration:none;line-height:25px;padding:0px 10px;display:inline-block;margin-right:5px;border:solid 1px #c8c7c7;} .page a:hover,.page a.checked{text-decoration:none;border:solid 1px #0086d6;background:#0091e3;color:#fff;} .page a:visited,.page a:link{color:#333;} .page a:active{color:#3B3B3B;} </style>css; echo $css; } public function md5($p,$c='webrx'){ $s1 = md5($p.$c); $s2 = sha1($p.$c); $sok = substr($s1,0,6).substr($s2,0,6); $sok .= substr($s1,12,5).substr($s2,22,5); $sok .= substr($s1,22,5).substr($s2,32,5); return $sok; } public function update($t,$d,$w='1=1'){ $t = $this->tbp.$t; foreach($d as $key=>$v){ $kk[] = $key.'=:'.$key; } $kk = implode(',',$kk); $stmt = $this->db->prepare("update $t set $kk where $w"); $stmt->execute($d); $stmt->closeCursor(); } public function __destruct(){ unset($this->db); }}