作者博客:漂小泊的博客
作者主站:小泊隨記
原文地址:文章地址
昨天實現(xiàn)了通過PHP向mysql中插入數(shù)據(jù)喘垂,今天咱們就實現(xiàn)通過php從mysql中將數(shù)據(jù)讀出來展現(xiàn)在頁面废离。
代碼:
話不多說侄泽,直接上代碼
<!DOCTYPE HTML>
<html>
<head>
<title>小泊隨記-留言板</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<meta name="keywords" content="小泊隨記,漂小泊,卞榮成,小泊隨記,漂小泊輕松學前端,piaoxiaobo,xiaobosuiji,隨記,隨記,itsolotime," />
<meta name="description" content="因為心無所恃,所以才隨遇而安??厅缺。一個孤身漂泊的90后程序員..." />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<link rel="apple-touch-icon" sizes="180x180" href="static/images/apple-touch-icon.png" />
<link rel="icon" type="image/png" sizes="32x32" href="static/images/favicon-16x16.png" />
<link rel="icon" type="image/png" sizes="16x16" href="static/images/favicon-32x32.png" />
<style type="text/css">
table{
border: 1px solid black;
border-color:#F06;
padding:10px;
border-collapse:collapse;
}
table tr th{
border: 1px solid black;
border-color:#F06;
padding:10px;
border-collapse:collapse;
}
table tr td{
border: 1px solid black;
border-color:#F06;
padding:10px;
border-collapse:collapse;
}
</style>
</head>
<body>
<table>
<caption>消息列表</caption>
<tr><th>編號</th><th>姓名</th><th>郵箱</th><th>留言</th></tr>
<?php
header("Content-Type:text/html;charset=utf-8");//設(shè)置頁面字符編碼
// 第一步蔬顾,連接數(shù)據(jù)庫
$conn = mysqli_connect('localhost','root','數(shù)據(jù)庫密碼');
// 第二步,選擇指定的數(shù)據(jù)庫湘捎,設(shè)置字符集
mysqli_select_db($conn,"message");
mysqli_query($conn,'SET NAMES UTF8');
// 顯示數(shù)據(jù)
$query = 'SELECT * FROM message_tal';
$result = mysqli_query($conn,$query);
// 把結(jié)果集轉(zhuǎn)換成數(shù)組賦值給$row,如果有數(shù)據(jù)為真
while(!!$row = mysqli_fetch_array($result)){
echo"<tr><td>".$row["id"]."</td><td>".$row["name"]."</td><td>".$row["email"]."</td><td>".$row["message"]."</td><tr>";
}
mysqli_close($conn);
?>
</table>
</body>
</html>
這里小泊需要說明的是诀豁,要區(qū)分mysql和mysqli的語法。此代碼最后文件的后綴為.php
PHP文件拒絕訪問
昨天小泊在測試的時候發(fā)現(xiàn)作為收集表單數(shù)據(jù)的php文件能夠被私自訪問窥妇,今天馬上修復了這個bug,在php文件頭部加入以下代碼可以拒絕訪問舷胜。
$fromurl="http://www.itsolotime.com"; //跳轉(zhuǎn)往這個地址。
if( $_SERVER['HTTP_REFERER'] == "" )
{
header("Location:".$fromurl); exit;
}
聯(lián)系小泊
email:643209899@qq.com