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

本文介绍如何使用PHP从MySQL数据库检索数据,并将其显示在HTML表格中,涵盖了数据库连接、查询操作及数据遍历的基本步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>

/*遍历获得的数组*/
<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值