index.php
$conn=mysql_connect("localhost","root","root")or dir("连接失败");
mysql_select_db("tb_demo",$conn);
$sql="select * from news";
$res=mysql_query($sql);
header("content-type:text/html;charset=utf-8");
echo "<h1>新闻列表</h1>";
echo "<a href='add_news.html'>添加新闻</a><hr/>";
echo "<table>";
echo "<tr><td>id</td><td>标题</td><td>查看详情</td><td>修改新闻</td></tr>";
while($row=mysql_fetch_assoc($res)){
echo "<tr><td>{$row['id']}</td><td>{$row['title']}</td><td><a href='show_new.php/look-id-{$row['id']}.shtml'>查看详情</a></td><td><a href='#'>修改页面</a></td></tr>";
}
//上面的红色的地址本来该是show_news.php?act=look&id={$row['id']}
echo "</table>";
//关闭资源
mysql_free_result($res);
mysql_close($conn);
show_new.php
header("Content-type:text/html;charset=utf-8");
$conn=mysql_connect("localhost","root","root");
mysql_select_db("tb_demo",$conn);
mysql_query("set names utf8");
$pa = $_SERVER['PATH_INFO'];
//$pa 打印出来的值是 /look-id-1.html
//通过正则表达式匹配获取的url地址
if(preg_match('/^\/(look)-(id)-([\d])\.shtml$/',$pa,$arr)){
$act = $arr[1]; //这个是请求的look方法
$id = $arr[3]; //这个是获取的id 值
$sql="select * from news where id= $id";
$res=mysql_query($sql);
$res = mysql_fetch_assoc($res);
echo $res['title']."<hr>".$res['content'];
}else{
echo "url地址不合法";
}
mysql_close($conn);