1 單例的數據庫工具類(./libs/Db.class.php)
class Db {
private static $obj = NULL ;
private $db_host ;
private $db_user ;
private $db_pass ;
private $db_name ;
private $charset ;
private function __construct ( $config ) {
$this - > db_host = $config [ 'db_host' ] ;
$this - > db_user = $config [ 'db_user' ] ;
$this - > db_pass = $config [ 'db_pass' ] ;
$this - > db_name = $config [ 'db_name' ] ;
$this - > charset = $config [ 'charset' ] ;
$this - > connectDb ( ) ;
$this - > selectDb ( ) ;
$this - > setCharset ( ) ;
}
private function __clone ( ) { }
public static function getInstance ( $config ) {
if ( ! self: : $obj instanceof self ) {
self: : $obj = new self ( $config ) ;
}
return self: : $obj ;
}
private function connectDb ( ) {
if ( ! @mysql_connect ( $this - > db_host , $this - > db_user , $this - > db_pass ) ) {
die ( "PHP連接MySQL服務器出錯!" ) ;
}
}
private function selectDb ( ) {
if ( ! mysql_select_db ( $this - > db_name ) ) {
die ( "選擇數據庫{ $this - > db_name } 失敗!" ) ;
}
}
private function setCharset ( ) {
$this - > exec ( "set names { $this - > charset } " ) ;
}
public function exec ( $sql ) {
$sql = strtolower ( $sql ) ;
if ( substr ( $sql , 0 , 6 ) == 'select' ) {
die ( "該方法不能執行select語句" ) ;
}
return mysql_query ( $sql ) ;
}
private function query ( $sql ) {
$sql = strtolower ( $sql ) ;
if ( substr ( $sql , 0 , 6 ) != 'select' ) {
die ( "該方法不能執行非select語句" ) ;
}
return mysql_query ( $sql ) ;
}
public function fetchOne ( $sql , $type = 3 ) {
$result = $this - > query ( $sql ) ;
$types = array (
1 = > MYSQL_NUM ,
2 = > MYSQL_BOTH ,
3 = > MYSQL_ASSOC
) ;
return mysql_fetch_array ( $result , $types [ $type ] ) ;
}
public function fetchAll ( $sql , $type = 3 ) {
$result = $this - > query ( $sql ) ;
$types = array (
1 = > MYSQL_NUM ,
2 = > MYSQL_BOTH ,
3 = > MYSQL_ASSOC
) ;
while ( $row = mysql_fetch_array ( $result , $types [ $type ] ) ) {
$arrs [ ] = $row ;
}
return $arrs ;
}
public function rowCount ( $sql ) {
$result = $this - > query ( $sql ) ;
return mysql_num_rows ( $result ) ;
}
}
2 連接數據庫的公共文件(./conn.php)
spl_autoload_register ( 'autoload' ) ;
function autoload ( $className ) {
$fileName = "./libs/$className .class.php" ;
if ( file_exists ( $fileName ) )
require_once ( $fileName ) ;
}
$arr = array (
'db_host' = > 'localhost' ,
'db_user' = > 'root' ,
'db_pass' = > '' ,
'db_name' = > 'newdb3' ,
'charset' = > 'utf8'
) ;
$db = Db: : getInstance ( $arr ) ;
var_dump ( $db ) ;
3 顯示學生信息列表
<?php
header ( "content-type:text/html;charset=utf-8" ) ;
require_once ( "./coon.php" ) ;
$sql = "select * from student order by id" ;
$arrs = $db - > fetchAll ( $sql ) ;
$records = $db - > rowCount ( $sql ) ;
?>
< ! DOCTYPE html>
< html lang= "en" >
< head>
< meta charset= "UTF-8" >
< title> Title< / title>
< / head>
< body>
< div style= "text-align: center;padding-bottom: 10px;" >
< h2> 學生信息管理中心< / h2>
< a href= "./add.php" > 添加學生< / a>
共< font color= "red" > <?php echo $records ; ?> < / font> 個學生
< / div>
< table width= "800" border= "1" bordercolor= "#ccc" rules= "all" align= "center" cellpadding= "5" >
< tr bgcolor= "#f0f0f0" >
< th> 編號< / th>
< th> 姓名< / th>
< th> 性別< / th>
< th> 年齡< / th>
< th> 學歷< / th>
< th> 工資< / th>
< th> 獎金< / th>
< th> 籍貫< / th>
< th> 操作選項< / th>
< / tr>
<?php
foreach ( $arrs as $arr ) { ?>
< tr align= "center" >
< td> <?php echo $arr [ 'id' ] ?> < / td>
< td> <?php echo $arr [ 'name' ] ?> < / td>
< td> <?php echo $arr [ 'sex' ] ?> < / td>
< td> <?php echo $arr [ 'age' ] ?> < / td>
< td> <?php echo $arr [ 'edu' ] ?> < / td>
< td> <?php echo $arr [ 'salary' ] ?> < / td>
< td> <?php echo $arr [ 'bonus' ] ?> < / td>
< td> <?php echo $arr [ 'city' ] ?> < / td>
< td>
< a href= "./edit.php?id=<?php echo $arr [ 'id' ] ?>" > 修改< / a>
< a href= "javascript:void (0)" onclick= "confirmDel ( <?php echo $arr [ 'id' ] ?> > 刪除< / a>
< / td>
< / tr>
<?php } ?>
< / table>
< / body>
< / html>
4 刪除一條記錄(./delete.php)
4.1 學生管理首頁(list.php)
4.2 delete.php
header ( "content-type:text/html;charset=utf-8" ) ;
require_once ( "./coon.php" ) ;
$id = $_GET [ 'id' ] ;
$sql = "DELETE FROM student WHERE id=$id " ;
if ( $db - > exec ( $sql ) ) {
echo "<h2>id={ $id } 的學生信息刪除成功!</h2>" ;
header ( "refresh:3;url=./list.php" ) ;
die ( ) ;
}
5 添加一條記錄(add.php)
5.1 添加的表單代碼
5.2處理表單提交的數據
<?php
header ( "content-type:text/html;charset=utf-8" ) ;
require_once ( "./coon.php" ) ;
if ( isset ( $_POST [ 'ac' ] ) && $_POST [ 'ac' ] == 'add' ) {
foreach ( $_POST as $k = > $v ) {
$$k = $v ;
}
$sql = "INSERT INTO student VALUES(NULL ,'$name ','$sex ',$age ,'$edu ',$salary ,$bonus ,'$city ')" ;
if ( $db - > exec ( $sql ) ) {
echo "<h2>學生信息添加成功!</h2>" ;
header ( "refresh:3;url=./list.php" ) ;
die ( ) ;
}
}
?>
6 創建一個分頁類(./libs/Pager.class.php)
<?php
final class Pager {
private $curPage ;
private $totalPage ;
public function __construct ( $curPage , $totalPage ) {
$this - > curPage = $curPage ;
$this - > totalPage = $totalPage ;
}
public function fenPagers ( ) {
$start = $this - > curPage - 5 ;
$end = $this - > curPage + 4 ;
if ( $this - > curPage < 6 ) {
$start = 1 ;
$end = $end + 6 - $this - > curPage ;
}
if ( $end > $this - > totalPage ) {
$start = $this - > totalPage - 10 + 1 ;
$end = $this - > totalPage ;
}
if ( $this - > totalPage <= 10 ) {
$start = 1 ;
$end = $this - > totalPage ;
}
$prev = $this - > curPage - 1 <= 1 ? 1 : $this - > curPage - 1 ;
echo "<a href=?curPage=$prev '>«</a>" ;
for ( $i = $start ; $i <= $end ; $i ++ ) {
if ( $i == $this - > curPage ) {
echo "<span class='current'>$i </span>" ;
} else {
echo "<a href='?curPage=$i '>$i </a>" ;
}
}
$next = $this - > curPage + 1 >= $this - > totalPage ? $this - > totalPage : $this - > curPage + 1 ;
echo "<a href='?curPage=$next '>»</a>" ;
}
}
7 列表頁分頁代碼
<?php
header ( "content-type:text/html;charset=utf-8" ) ;
require_once ( "./coon.php" ) ;
$pageSize = 5 ;
$curPage = isset ( $_GET [ 'curPage' ] ) ? $_GET [ 'curPage' ] : 1 ;
$startRow = ( $curPage - 1 ) * $pageSize ;
$sql = "SELECT * FROM student" ;
$records = $db - > rowCount ( $sql ) ;
$totalPage = ceil ( $records / $pageSize ) ;
$sql . = " ORDER BY id LIMIT { $startRow } ,{ $pageSize } " ;
$arrs = $db - > fetchAll ( $sql ) ;
< tr>
< td colspan= "9" align= "center" class = "pagelist" >
<?php
$pageObj = new Pager ( $curPage , $totalPage ) ;
$pageObj - > fenPagers ( ) ;
?>
< / td>
< / tr>