1 <?php 2 class SqlHelper{ 3 private $conn; 4 private static $host="localhost"; 5 private static $user="root"; 6 private static $password=""; 7 private static $db="emp"; 8 //构造函数 完成初始化的任务 9 public function __construct(){ 10 $this->conn= new mysqli(self::$host,self::$user,self::$password,self::$db); 11 if ($this->conn->connect_error){ 12 die("连接失败".$this->conn->connect_error); 13 14 } 15 $this->conn->query("set names utf8"); 16 17 } 18 19 20 21 //执行dql查询语句 22 public function execut_dql($sql){ 23 $res=$this->conn->query($sql) or 24 die('sql语句错误'.$this->conn->error); 25 26 //关闭连接 27 $this->conn->close(); 28 29 return $res; 30 //释放资源 31 $res->free(); 32 33 } 34 //执行dml语句 35 public function execut_dml($sql){ 36 37 //操作$sql 38 $res=$this->conn->query($sql) or die("sql语句错误".$this->conn->error); 39 40 41 if (!$res){ 42 return 0; //操作失败 43 }else{ 44 if($this->conn->affected_rows>0){ 45 return 1; //操作成功 46 }else { 47 return 2; //没有受影响的行数 48 } 49 50 51 52 } 53 //关闭连接 54 $this->conn->close(); 55 //释放资源 56 $res->free(); 57 58 } 59 public function execut_dql2($id){ 60 $res=$this->conn->query($id) or die("sql语句错误".$this->conn->error);; 61 //查询的结果放进数组中 62 $emps=array(); 63 64 while($row=$res->fetch_assoc()){ 65 $emps[]=$row; 66 67 } 68 69 //关闭资源 70 $res->free(); 71 //关闭连接 72 $this->conn->close(); 73 return $emps; 74 } 75 //执行分页的的sql语句,并赋值给fenyepage类 76 public function execut_dql_fenye($sql1,$sql2,$fenyePage){ 77 //执行要查询的语句 $res1返回一个结果集 78 $res1=$this->conn->query($sql1) or die("sql语句错误".$this->conn->error); 79 //用数组接收取出的值 80 $arr= array(); 81 //取出结果集每一行,转移到数组中 82 while ($row=$res1->fetch_assoc()){ 83 $arr[]=$row; 84 } 85 //释放资源 86 $res1->free(); 87 88 $fenyePage->arr=$arr; 89 90 $res2=$this->conn->query($sql2) or die("sql语句错误".$this->conn->error); 91 92 $count=$res2->fetch_assoc(); 93 $rowCount=$count['c']; 94 95 $fenyePage->rowCount=$rowCount; 96 $fenyePage->pageCount=ceil($rowCount/$fenyePage->pageSize); 97 //释放资源 98 $res2->free(); 99 //关闭连接 100 $this->conn->close(); 101 //如果点击当前页大于1使用当前页-1,小于使用1 102 $prev= $fenyePage->pageNow>1 ? $fenyePage->pageNow-1:1; 103 //如果点击当前页小于总页,当前页加一,否则取总页 104 $next= $fenyePage->pageNow<$fenyePage->pageCount ? $fenyePage->pageNow+1:$fenyePage->pageCount; 105 106 $pageMix=$fenyePage->pageNow-$fenyePage->pageSpa<1 ? 1:$fenyePage->pageNow-$fenyePage->pageSpa; 107 $pageMax=$fenyePage->pageNow+$fenyePage->pageSpa-1<10 ? 10:$fenyePage->pageNow+$fenyePage->pageSpa-1; 108 $pageMaxd= $pageMax>$fenyePage->pageCount?$fenyePage->pageCount:$pageMax; 109 //首页 110 $fenyePage->naviga="<a href='$fenyePage->url?pageNow=1'>首页</a> "; 111 //快速翻页,如果点击的页面小于10,隐藏 112 if($fenyePage->pageNow>10){ 113 $fenyePage->naviga.="<a href='$fenyePage->url?pageNow=".($pageMix-5)."'><<</a> "; 114 } 115 if($fenyePage->pageNow!=1){ 116 117 //页面不等于1显示页面,否则不显示上一页 118 119 $fenyePage->naviga.= "<a href='$fenyePage->url?pageNow=$prev'>上一页</a> "; 120 } 121 122 123 for($pageMix;$pageMix<=$pageMaxd;$pageMix++){ 124 125 $fenyePage->naviga.="<a href='$fenyePage->url?pageNow=$pageMix'>$pageMix</a> "; 126 127 } 128 if($fenyePage->pageNow < $fenyePage->pageCount){ 129 $fenyePage->naviga.="<a href='$fenyePage->url?pageNow=$next'>下一页</a> "; 130 } 131 if($fenyePage->pageNow+5<$fenyePage->pageCount){ 132 $fenyePage->naviga.="<a href='$fenyePage->url?pageNow=".($pageMaxd+$fenyePage->pageSpa+1)."'>>></a> "; 133 } 134 $fenyePage->naviga.="<a href='$fenyePage->url?pageNow=$fenyePage->pageCount'>末页</a> "; 135 $fenyePage->naviga.="当前第{$fenyePage->pageNow}页/总共{$fenyePage->pageCount}页<br/><br/>"; 136 } 137 138 139 } 140 ?>