您好,每当我尝试重置行以便我可以为新列使用相同的行时,我会收到错误:reset()期望参数1为数组
我是PHP新手并且正在尝试构建一个输出存储在MySQL中的数据的表:
<?php
include ('config.php');
$user = mysqli_query($userDB, "SELECT username, email FROM user") or die(mysqli_error());
?>
<table width="200" border="1">
<tbody>
<tr>
<th scope="col">Username</th>
<th scope="col">Email</th>
</tr>
<tr>
<td align = "center">
<?php while($row = mysqli_fetch_assoc($user)) echo $row["username"]."<br>"; reset($row); ?>
</td>
<td align = "center">
<?php while($row = mysqli_fetch_assoc($user)) echo $row["email"]."<br>" ?>
</td>
</tr>
</tbody>
</table>
我能做得更好吗?
答案 0 :(得分:1)
你可以用更好的方式重写:
<table width="200" border="1">
<thead>
<tr>
<th scope="col">Username</th>
<th scope="col">Email</th>
</tr>
</thead>
<tbody>
<?php while($row = mysqli_fetch_assoc($user)): ?>
<tr>
<td align = "center">
<?php echo $row["username"]; ?>
</td>
<td align = "center">
<?php echo $row["email"]; ?>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
除非您需要将所有用户都放在一个<td>