I know its a super basic question but I can't find an answer all day. I'm trying to display all of my database rows in a table and my while loop only displays the first row.
I tried to change the condition of the while loop to "mysqli_fetch_array" and then not even the first row showed up.
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>";
Expected result: table with all rows from database. Actual result: only the first row from database
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";
$result = sqlsrv_query($conn, $sql);
while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
$id = $row['id'];
$Home = $row['Home'];
$Away = $row['Away'];
$result = $row['result'];
$season = $row['season'];
$notes = $row['notes'];
$home_goals= $row['home_goals'];
$away_goals= $row['away_goals'];
$GoalSum = $home_goals+$away_goals;
echo $output.="<tr>
<td>$id</td>
<td>$Home</td>
<td>$Away</td>
<td>$result</td>
<td>$season</td>
<td>$notes</td>
<td>$GoalSum</td>
</tr>";
}
echo "</table>";
Your code sample looks cleans at first sight but this still reminds me of two distinct possibilities :
while()
condition: while($res=fetch(…));
{
display something…
}
In this case, the loop will iterate until the end of your dataset, then the block in curly brackets (assumed to be orphan) will be entered, displaying what's remaining in the variables. But in this case, you should get the last row of your query, not the first one.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.