读取数据库需要利用php中的函数连接数据库然后再利用sql查 询表中的数据再遍历出来即可。
下面是一些简单的代码
```
<?php
//我们首先来连接准备好的数据库
$conn = @new mysqli('localhost','root','','myschool');
//有连接就有可能会有失败
if($conn ->connect_error){
// show_error('连接数据库失败','register.html');//register.html
是我们 的网页
die('连接数据库失败');
}
//由于环境问题我们可能需要设置编码格式
$conn->set_charset('utf8');
//下面来创建sql指令
$sql="select id,user,pwd,create_time from user";
//然后执行sql
$result = $conn ->query($sql);
if($result ===false){
die('执行sql出错');
}
$arr = array();
while($row =$result->fetch_array()){
// print_r($row);
$arr[]=$row;
}
1>fetch_row()输出数字索引
2>fetch_assoc()输出键名
3>fetch_array()会重复输出,有索引号也有键名
最常用的是最后两个
//释放结果集
$result ->free();
//关别数据库连接
$conn->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<table border="1">
<tr>
<td>用户名</td>
<td>出生日期</td>
</tr>
<?php foreach($arr as $row){?>
<tr>
<td><?php echo $row['user'] ?></td>
<td><?php echo date('Y-m-d H:i:s',$row['create_time']) ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
```
07-13
07-13
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交