php从数据库获取数据并遍历在表格中

<?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>

猜你喜欢

转载自www.cnblogs.com/chalkbox/p/11657288.html