<?php
/*連接數據庫並以一個數組的形式獲得數據*/
header("Content-type:text/html;charset=UTF-8");
$con = mysqli_connect('localhost','root','','login');
mysqli_set_charset($con,'utf8');
if(!$con){
die('Could not connect:' . mysql_error($con));
}
$sql = "select * from file";
$result = mysqli_query($con,$sql);
$rows = array();
while($row = mysqli_fetch_assoc($result)){
$rows[] = $row;
}
?><!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>員工檔案</title>
<link rel="stylesheet" href="style.css" />
</head><table>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>職位</th>
<th>身份證號</th>
<th>身份證地址</th>
<th>性別</th>
<th>入職日期</th>
<th>手機號</th>
</tr>
</thead>
<tbody>/*遍歷獲得的數組*/
<?php foreach($rows as $key => $v) {?>
<tr>
<td><?php echo $v['id'];?></td>
<td><?php echo $v['name'];?></td>
<td><?php echo $v['job'];?></td>
<td><?php echo $v['number'];?></td>
<td><?php echo $v['address'];?></td>
<td><?php echo $v['sex'];?></td>
<td><?php echo $v['time'];?></td>
<td><?php echo $v['cell'];?></td>
</tr>
<?php };?>
</tbody>
</table></body>
</html>
這里只寫了php從數據庫獲取內容省略了html——ajax,如要了解可以看AJAX官方教程
從數據庫讀取數據原理:
簡單原理:1. html前台頁面通過JS文件(ajax)請求php文件,2. php文件讀取數據庫內容獲得想要的數據。3.php文件獲得數據庫內容並顯示
詳細:一:html定義一個空的div用於顯示數據庫的內容
二:ajax獲得html定義的div段落,並獲得輸入內容Q
三:ajax將Q傳遞給php文件
四:php文件根據Q值從數據庫中讀取數據
五:php文件將讀取到的數據輸出
六:ajax獲得php輸出內容,將輸出內容顯示在div段落中
附上一張圖

