1. <?php
  2. /**
  3. *
  4. */
  5. class Db{
  6. private $host = '';
  7. private $port = '';
  8. private $user = '';
  9. private $pass = '';
  10. private $dbname = '';
  11. private $charset='';
  12. private $tbp = '';
  13. private $db = '';
  14. /**
  15. * 返回 pdo对象
  16. */
  17. public function getDb(){
  18. return $this->db;
  19. }
  20. /**
  21. * @param $tbp 表名前辍
  22. */
  23. public function setTbp($tbp=''){
  24. $this->tbp = $tbp;
  25. }
  26. public function __construct($host='localhost',$port='3306',$user='root',$pass='',$dbname='fzdb',$charset='utf8',$tbp='fz_'){
  27. $dsn = "mysql:host=$host;port=$port;dbname=$dbname";
  28. $opt = [1002 =>"set names $charset"];
  29. try {
  30. $this->db = new PDO ( $dsn, 'root', '', $opt );
  31. $this->host = $host;
  32. $this->port = $port;
  33. $this->user = $user;
  34. $this->pass = $pass;
  35. $this->dbname = $dbname;
  36. $this->tbp = $tbp;
  37. $this->charset = $charset;
  38. } catch ( Exception $e ) {
  39. exit ( '数据库服务器连接失败,程序终止' );
  40. }
  41. }
  42. /**
  43. *
  44. * @param unknown $t 表名,不含执行时会自动加上表前辍
  45. * @param unknown $d 要插入的数据是关联数组
  46. */
  47. public function gomsg($url='',$msg=''){
  48. echo '<script>';
  49. echo "alert('$msg');";
  50. echo "location.href='$url'";
  51. echo '</script>';
  52. }
  53. public function save($t,$d){
  54. $t = $this->tbp.$t;
  55. foreach($d as $key=>$v){
  56. $kk[] = $key;
  57. $vv[] = ':'.$key;
  58. }
  59. $kk = implode(',',$kk);
  60. $vv = implode(',',$vv);
  61. $stmt = $this->db->prepare("insert into $t($kk) values($vv)");
  62. $stmt->execute($d);
  63. $stmt->closeCursor();
  64. }
  65. public function query($t,$f='*',$w='1=1',$o='',$l='limit 10'){
  66. $t = $this->tbp.$t;
  67. $stmt = $this->db->prepare("select $f from $t where $w $o $l");
  68. $stmt->execute();
  69. $rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
  70. $stmt->closeCursor();
  71. return $rs;
  72. }
  73. public function delete($t,$w='1=1'){
  74. $t = $this->tbp.$t;
  75. $stmt = $this->db->prepare("delete from $t where $w");
  76. $stmt->execute();
  77. $stmt->closeCursor();
  78. }
  79. public function getcount($t,$w='1=1'){
  80. $t = $this->tbp.$t;
  81. $stmt = $this->db->prepare("select count(*) from $t where $w");
  82. $stmt->execute();
  83. $stmt->bindColumn(1, $c);
  84. $stmt->fetchAll(PDO::FETCH_NUM);
  85. return $c;
  86. }
  87. public function updatenum($t,$f,$w='1=1'){
  88. $t = $this->tbp.$t;
  89. $stmt = $this->db->prepare("update $t set $f where $w");
  90. $stmt->execute();
  91. $stmt->closeCursor();
  92. }
  93. public function pager($t,$currpage=1,$f='*',$pagesize=3,$w='1=1',$o='',$ty=''){
  94. $recordcount = $this->getcount($t,$w);
  95. $t = $this->tbp.$t;
  96. $stmt = $this->db->prepare("select $f from $t where $w $o limit ?,?");
  97. $pagecount = ceil($recordcount/$pagesize);
  98. $currpage = $currpage<1 ? 1 : $currpage;
  99. $currpage = $currpage>$pagecount ? $pagecount : $currpage;
  100. $start = $currpage*$pagesize - $pagesize;
  101. $stmt->bindParam(1, $start, PDO::PARAM_INT);
  102. $stmt->bindParam(2, $pagesize, PDO::PARAM_INT);
  103. $stmt->execute();
  104. $row[] = $stmt->fetchAll(PDO::FETCH_ASSOC);
  105. $first = 1;
  106. $end = 10;
  107. $pages = '<div class="page">';
  108. if($currpage>=7){
  109. $first = $currpage-5;
  110. $end = $first+$end-1;
  111. }
  112. if($currpage>1){
  113. $prev = $currpage-1;
  114. if($first>1){
  115. $pages.="<a href=?".$ty."p=1>首页</a><a href=?".$ty."p=$prev>上一页</a>";
  116. }else{
  117. $pages.="<a href=?".$ty."p=$prev>上一页</a>";
  118. }
  119. }
  120. for($i=$first;$i<=$end;$i++){
  121. if($i>$pagecount){
  122. break;
  123. }
  124. if($i==$currpage){
  125. $pages.='<a class="checked">'.$i.'</a>';
  126. continue;
  127. }
  128. $pages.="<a href=?".$ty."p=$i>$i</a>";
  129. }
  130. if($currpage<$pagecount){
  131. $next = $currpage+1;
  132. $pages.="<a href=?".$ty."p=$next>下一页</a>";
  133. }
  134. if($end<$pagecount){
  135. $pages.="<a href=?".$ty."p=$pagecount>尾页</a>";
  136. }
  137. $row[] = $pages.'</div>';
  138. $row[] = $pagesize;
  139. $row[] = $pagecount;
  140. $row[] = $recordcount;
  141. $row[] = $currpage;
  142. return $row;
  143. }
  144. public function css1(){
  145. $css = <<<css
  146. <style>
  147. .page{font-size:12px;height:30px;padding:15px 0;clear:both;overflow:hidden;text-align:center;}
  148. .page a{text-decoration:none;line-height:25px;padding:0px 10px;display:inline-block;margin-right:5px;border:solid 1px #c8c7c7;}
  149. .page a:hover,.page a.checked{text-decoration:none;border:solid 1px #0086d6;background:#0091e3;color:#fff;}
  150. .page a:visited,.page a:link{color:#333;}
  151. .page a:active{color:#3B3B3B;}
  152. </style>
  153. css;
  154. echo $css;
  155. }
  156. public function md5($p,$c='webrx'){
  157. $s1 = md5($p.$c);
  158. $s2 = sha1($p.$c);
  159. $sok = substr($s1,0,6).substr($s2,0,6);
  160. $sok .= substr($s1,12,5).substr($s2,22,5);
  161. $sok .= substr($s1,22,5).substr($s2,32,5);
  162. return $sok;
  163. }
  164. public function update($t,$d,$w='1=1'){
  165. $t = $this->tbp.$t;
  166. foreach($d as $key=>$v){
  167. $kk[] = $key.'=:'.$key;
  168. }
  169. $kk = implode(',',$kk);
  170. $stmt = $this->db->prepare("update $t set $kk where $w");
  171. $stmt->execute($d);
  172. $stmt->closeCursor();
  173. }
  174. public function __destruct(){
  175. unset($this->db);
  176. }
  177. }