0

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

3
  • Why are you using a while loop in the first place? This is the kind of problem for loops were designed for. Commented May 9, 2019 at 18:34
  • Can you give an example how I can do this with a for loop? Commented May 9, 2019 at 18:51
  • your code have syntax error.did you paste it correctly ? Commented May 9, 2019 at 19:57

2 Answers 2

0
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>";
Sign up to request clarification or add additional context in comments.

6 Comments

I did and it was a mistake, so I removed it. Sorry about this. However, I don't see in what your proposition is different from the OP one…
Copied your code, and still only one row is displayed.
Is the condition in the while loop ok?
remove SQLSRV_FETCH_ASSOC from while loop & try it.
didn't change anything
|
0

Your code sample looks cleans at first sight but this still reminds me of two distinct possibilities :

  • Your table only contains one row. It's silly but it must be checked first. Well, I supposed you did ;
  • An extraneous semi-colon ";" forgotten after a 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.

1 Comment

1. My table contains more that one row. 2. For sure it displays the first row, so it's not the the semi-colon, even though I tried to add it and then no row showed up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.