我正在尝试做一个学校项目,结果只显示我的MySQL表中的1个数据。这是代码:
<?php
include("connect.php");
$view_users_query= "select * from data_cuti";
$run= mysql_query($view_users_query);
while($row= mysql_fetch_array($run)){
$name=$row['Name'];
$startdate=$row['startdate'];
$finaldate=$row['finaldate'];
$within=$row['within'];
$reason=$row['reason'];
$remaining=$row['remaining'];
$status=$row['status'];
$id=$row['id'];
}
?>
<tr>
<td><?php echo $name; ?></td>
<td><?php echo $startdate; ?></td>
<td><?php echo $finaldate; ?></td>
<td><?php echo $within; ?></td>
<td><?php echo $reason; ?></td>
<td><?php echo $remaining; ?></td>
<td><?php echo $status; ?></td>
</tr>
</table>
任何人都可以帮忙告诉我,我错过了什么?
答案 0 :(得分:2)
正如其他评论者所说,你需要在while循环中包含输出块 ,否则你只能从最后一行获得输出。
试试这个:
<table>
<?php
include("connect.php");
$view_users_query= "select * from data_cuti";
$run= mysql_query($view_users_query);
while($row= mysql_fetch_array($run)){
$name=$row['Name'];
$startdate=$row['startdate'];
$finaldate=$row['finaldate'];
$within=$row['within'];
$reason=$row['reason'];
$remaining=$row['remaining'];
$status=$row['status'];
$id=$row['id'];
?>
<tr>
<td><?php echo $name; ?></td>
<td><?php echo $startdate; ?></td>
<td><?php echo $finaldate; ?></td>
<td><?php echo $within; ?></td>
<td><?php echo $reason; ?></td>
<td><?php echo $remaining; ?></td>
<td><?php echo $status; ?></td>
</tr>
<?php } ?>
</table>
您使用的所有中间变量都是多余的,因此这相当于更短:
<table>
<?php
include("connect.php");
$view_users_query= "select * from data_cuti";
$run= mysql_query($view_users_query);
while($row= mysql_fetch_array($run)){
?>
<tr>
<td><?php echo $row['Name']; ?></td>
<td><?php echo $row['startdate']; ?></td>
<td><?php echo $row['finaldate']; ?></td>
<td><?php echo $row['within']; ?></td>
<td><?php echo $row['reason']; ?></td>
<td><?php echo $row['remaining']; ?></td>
<td><?php echo $row['status']; ?></td>
</tr>
<?php } ?>
</table>
或者更简洁,没有html和php模式之间的混乱切换:
<?php
include("connect.php");
$view_users_query= "select * from data_cuti";
$run= mysql_query($view_users_query);
echo "<table>";
while($row= mysql_fetch_array($run)){
echo "<tr>";
echo "<td>$row['Name']</td>";
echo "<td>$row['startdate']</td>";
echo "<td>$row['finaldate']</td> ";
echo "<td>$row['within']</td>";
echo "<td>$row['reason']</td>";
echo "<td>$row['remaining']</td>";
echo "<td>$row['status']</td>";
echo "</tr>";
}
echo "</table>";
答案 1 :(得分:0)
如果您也想要标题,可以执行以下操作:
<?php include("connect.php");?>
<table>
<tr>
<th>Name</th>
<th>Start Date</th>
<th>Final Date</th>
<th>Within</th>
<th>Reason</th>
<th>Remaining</th>
<th>Status</th>
</tr>
<?php
$view_users_query= "select * from data_cuti";
$run= mysql_query($view_users_query) or die(mysql_error());
?>
<?php
while($row= mysql_fetch_array($run)){
?>
<tr>
<td><?php echo $row['Name']; ?></td>
<td><?php echo $row['startdate']; ?></td>
<td><?php echo $row['finaldate']; ?></td>
<td><?php echo $row['within']; ?></td>
<td><?php echo $row['reason']; ?></td>
<td><?php echo $row['remaining']; ?></td>
<td><?php echo $row['status']; ?></td>
</tr>
<?php } ?>
</table>