PHP数据库链接类

db.php

 

<?php
if (!isset ($_SESSION)) {
	session_start();
}
define('DB_HOST', '127.0.0.1');   //根据自己的情况填写
define('DB_USER', 'root');
define('DB_PASS', 'a123789');
define('DB_NAME', 'db1');
date_default_timezone_set('Asia/Shanghai');

//获取一个数据库连接
function getDBLink() {
	$link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
	mysql_select_db(DB_NAME, $link);
	mysql_query("set names utf8");
	return $link;
}
?>

 

 

 

index.php

 

 

<?php
if($_SESSION['pass']=="ok")                 
{

require_once ('db.php');
$link=getDBLink();
$sql="select * from member";
$result=mysql_query($sql);
$total=mysql_num_rows($result);
echo "<center>共有".$total."个会员</center><br>";
$page=isset($_GET['page'])?intval($_GET['page']):1;  
$info_num=3; 
$pagenum=ceil($total/$info_num); 
If($page>$pagenum || $page == 0){
       Echo "Error : Can Not Found The page .";
       Exit;
}
$offset=($page-1)*$info_num; 
$info=mysql_query("select * from member limit $offset,$info_num"); 
while($it=mysql_fetch_array($info)){
       Echo "账号:  ".HTMLSpecialChars($it['member_account'])."<br>";   //防sql注入
	   Echo "姓名:  ".HTMLSpecialChars($it['member_name'])."<br>";
	   Echo "性别:  ".HTMLSpecialChars($it['sex'])."<br>";
	   Echo "学号:  ".HTMLSpecialChars($it['schoolnumber'])."<br>";
	   Echo "手机号码:".HTMLSpecialChars($it['phonenumber'])."<br>";
	   Echo "电子邮箱:".HTMLSpecialChars($it['email'])."<hr>";
}  
if( $page > 1 )
    {
    	echo "<a href='member_index.php?page=".($page-1)."'>前一页</a>&nbsp";
	}else{
   	echo "前一页&nbsp&nbsp";
}
for($i=1;$i<=$pagenum;$i++){                                          //数字页面
       $show=($i!=$page)?"<a href='member_index.php?page=".$i."'>".$i."</a>":"$i";
       Echo $show." ";
}
if( $page<$pagenum)
    {
    	echo "<a href='member_index.php?page=".($page+1)."'>后一页</a>";
	}else
	{
		echo "后一页";
     }
mysql_free_result($result);
mysql_close();
}
?>

你可能感兴趣的:(数据库链接)