【发布时间】:2019-09-27 14:35:49
【问题描述】:
我知道这是一个超级基本的问题,但我一整天都找不到答案。 我试图在一个表中显示我所有的数据库行,而我的 while 循环只显示第一行。
我尝试将 while 循环的条件更改为“mysqli_fetch_array”,然后甚至第一行都没有出现。
echo "<table>";
echo "<tr>
<th>Match_Id</th>
<th>Home_Team</th>
<th>Away_Team</th>
<th>Result</th>
<th>season</th>
<th>notes</th>
<th>Goals_Sum</th>
</tr> ";
$sql = "SELECT * FROM PremierLeague";
`enter code here` $result = sqlsrv_query($conn, $sql);
while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
echo "<tr>";
$id = $row['id'];
echo "<td>$id</td>";
$Home = $row['Home'];
echo "<td>$Home</td>";
$Away = $row['Away'];
echo "<td>$Away</td>";
$result = $row['result'];
echo "<td>$result</td>";
$season = $row['season'];
echo "<td>$season</td>";
$notes = $row['notes'];
echo "<td>$notes</td>";
$home_goals= $row['home_goals'];
$away_goals= $row['away_goals'];
$GoalSum = $home_goals+$away_goals;
echo "<td>$GoalSum</td>";
echo "</tr>";
}
echo "</table>";
预期结果:包含数据库中所有行的表。 实际结果:只有数据库的第一行
【问题讨论】:
-
你为什么首先使用while循环?这是为循环设计的问题。
-
你能举个例子,我如何用 for 循环做到这一点?
-
您的代码有语法错误。您粘贴正确了吗?