While 循环只显示数据库中的第一行

我知道这是一个超级基本的问题,但我一整天都找不到答案。我试图在表中显示我的所有数据库行,而我的 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>";

预期结果:包含数据库中所有行的表。实际结果:只有数据库的第一行


慕仙森
浏览 266回答 2
2回答

波斯汪

echo "<table>";&nbsp; &nbsp; echo "<tr>&nbsp; &nbsp; &nbsp; &nbsp; <th>Match_Id</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>Home_Team</th>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <th>Away_Team</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>Result</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>season</th>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <th>notes</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>Goals_Sum</th>&nbsp; &nbsp; &nbsp; </tr> ";&nbsp; &nbsp; &nbsp;$sql = "SELECT * FROM PremierLeague";&nbsp; $result = sqlsrv_query($conn, $sql);&nbsp;while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $id = $row['id'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $Home = $row['Home'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $Away = $row['Away'];&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $result = $row['result'];&nbsp; &nbsp; &nbsp; &nbsp;$season = $row['season'];&nbsp; &nbsp; &nbsp; &nbsp;$notes = $row['notes'];&nbsp; &nbsp; &nbsp;$home_goals= $row['home_goals'];&nbsp; &nbsp; &nbsp;$away_goals= $row['away_goals'];&nbsp; &nbsp; &nbsp;$GoalSum = $home_goals+$away_goals;&nbsp;echo $output.="<tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>$id</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>$Home</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>$Away</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>$result</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>$season</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>$notes</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>$GoalSum</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>";&nbsp;}&nbsp; echo "</table>";

元芳怎么了

您的代码示例乍一看很干净,但这仍然让我想起两种不同的可能性:您的表格仅包含一行。这很愚蠢,但必须先检查。好吧,我想你做到了;一个无关的分号“;” while()条件后忘记:&nbsp; &nbsp; while($res=fetch(…));&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; display something…&nbsp; &nbsp; }在这种情况下,循环将迭代直到数据集结束,然后将输入大括号中的块(假设为孤立块),显示变量中剩余的内容。但在这种情况下,您应该获得查询的最后一行,而不是第一行。
打开App,查看更多内容
随时随地看视频慕课网APP