前面我們提到設定一個使用資料庫的類別LyDB,
這裡就把所有的SQL和分頁功能進行封裝,後面直接呼叫即可:
<?php class LyDB{ var $_host="localhost"; var $_user="root";// var $_password="root"; var $_database="test"; var $page_size=3; var $link; public function __construct(){ //连接数据库 date_default_timezone_set('PRC'); $this->link = mysqli_connect($this->_host,$this->_user,$this->_password,$this->_database); if (!$this->link) { die('Could not connect to MySQL: ' . mysqli_connect_error()); } } public function __destruct(){ mysqli_close($this->link); } public function insert($nickname,$avatar,$message) //添加留言内容到数据库 { $message=str_replace ("<" , "<" , $message); //str_replace() 函数以其他字符替换字符串中的一些字符(区分大小写)。 $message=str_replace (">" , ">" , $message); $message=str_replace ("\n" , " " , $message); $message=trim($message); //trim() 函数移除字符串两侧的空白字符或其他预定义字符。 $lytime=date("Y-m-d H:i:s"); $sql="insert into ly (nickname,message,avatar,lytime)values('$nickname','$message','$avatar','$lytime')"; $query=mysqli_query($this->link,$sql); if($query){ return true; } else { return false; } } public function reply($id,$reply){ $message=str_replace ("<" , "<" , $reply); $message=str_replace (">" , ">" , $message); $message=str_replace ("\n" , " " , $message); $message=trim($message); $reply=$message; $retime=date("Y-m-d H:i:s"); $sql="update ly set reply='$reply', retime='$retime' where id=$id"; $query=mysqli_query($this->link,$sql); if($query)return true; else return false; } public function delete_by_id($id){ //删除留言 $sql="delete from ly where id=$id"; mysqli_query($this->link,$sql); } public function select_page_info(){ //分页 $sql = "select count(*) as pagecount from ly"; $query = mysqli_query($this->link,$sql); $row = mysqli_fetch_assoc($query); $ly_count=$row["pagecount"]; $page_count=($ly_count%$this->page_size==0)?($ly_count/$this->page_size):($ly_count/$this->page_size+1); $page_info=array("ly_count"=> $ly_count,"page_count"=>(int)$page_count,"page_size"=>$this->page_size); return $page_info; } public function select_all_result(){ //查询显示留言 $sql="select * from ly order by id desc"; $query=mysqli_query($this->link,$sql); return $query; } public function select_page_result(&$page_no){ //上一页,下一页,首页,末页 $page_info=$this->select_page_info(); if(!isset($page_no))$page_no=1; else if($page_no<1)$page_no=1; else if($page_no>$page_info["page_count"])$page_no=$page_info["page_count"]; $first=($page_no-1)*$this->page_size; $sql="select * from ly order by id desc limit $first,$this->page_size "; $query=mysqli_query($this->link,$sql); $page_result=array("page_data"=>$query,"page_info"=>$page_info,"page_no"=>$page_no); return $page_result; } } ?>
給這個類別文件一個名稱:lydb.class.php
管理員登入和權限判斷也建立了一個呼叫的類別authority.class.php檔案
<?php class Authority{ static public function check_insert(){ //声明公共变量 //检查是否具有添加留言权限 return true; } static public function check_delete(){ //检查是否具有delete权限 if(isset($_SESSION["username"]) && $_SESSION["username"]=="admin") return true; else return false; } } ?> <?php class User{ static public function validate($username,$password){ // static public 声明公共变量 if("admin"==$username && "12345"==$password){ return true; } else return false; } } ?>
然後在前端頁面直接引用即可:
<?php require_once("lydb.class.php");?> <?php require_once("authority.class.php");?>
require_once()語句在腳本執行期間包含並執行指定檔案(通俗一點,括號內的檔案會執行一遍)。此行為和require()語句類似,唯一差異是如果該檔案中的程式碼已經被包含了,則不會再次包含。
下一節