本来想着这样的代码会将数据库里面的数据的读取并展示出来,但奇怪的是总只是返回第一条数据,是我的循环设置错误了?还请指点为感,谢谢!
<?php
//这段代码对未通过会员认证的人进行了跳转登录页面的处理
if($_COOKIE['userRight']!= 'normalRight'){
header('location:loginPage.php');
exit();
}
require_once('dataBase.php');//create the connection to database
$userName=$_COOKIE['userName'];
$sql="SELECT * FROM result_table WHERE userName='$userName' ";
$result=$conn->query($sql);//we get all record in this step
$recordNumber=$result->num_rows;//we get the total number of the records'rows which we need
echo $recordNumber;
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>历史测评结果展示页面</title>
<style>
div{
width: 500px;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
table{
font-size: 30px;
text-align: center;
}
td{
white-space: nowrap;
}
</style>
</head>
<body>
测评人
测评时间
测评序列
<?php
echo $recordNumber;
for($i=1;$i<=$recordNumber;$i++){
$sql="SELECT * FROM result_table WHERE userName='{$userName}' ";//用{}可以更好的表示被引用的是个变量,据说可以防止sql注入
$result=$conn->query($sql);//we get all record in this step
$row=$result->fetch_assoc();//we get the last record
$testDate=$row['testDate'];
$testTime=$row['testTime'];
echo $testTime;
echo <<<EOF
$userName
$testDate
$testTime
EOF;
}
$conn->close();
?>
</body>
</html>