如何在php表中显示sql中的每一行

时间:2019-06-19 20:39:05

标签: php sql

我在模态中有一张桌子。我希望表格显示足球比赛中排名前三的进球者。以下代码不返回表。任何帮助,不胜感激!目前,我正在尝试在php标记中回显该表,但是我不确定这是否是执行此操作的最佳方法,因此我采用了这种方法,因为我希望从数据库中返回三行。

<div class="modal fade bd-example-modal-lg" id="homeleaderboard" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">

  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <h3 style="text-align: center;">Most Goals</h3>


      <?php
      $topgoals="SELECT player,panel.name,count(*) AS goalcount FROM fixtures
      inner join panel
      on fixtures.player=panel.id
      where fixture='$fixture_id' and goal='1'
      group by player
      order by goalcount desc
      limit 3";

      $goalsresult=mysqli_query($conn,$topgoals) or die(mysqli_error($conn));

      if(mysqli_num_rows($result)>0){
        while($row=mysqli_fetch_assoc($result)){
          $goalnames=$row['name'];
          $goaltotal=$row['goalcount'];
          echo "<div class='table-responsive'>
                <table class='table table-striped table-bordered'>
                <thead>
                <tr>
                <th>Player</th>
                <th>Goals</th>
                </tr>
                </thead>
                <tr>
                <td>$goalnames</td>
                <th>$goaltotal</th>


                </tr>
                </table>
                </div>

                  ";
      }
        }

       ?>

  </div>
</div>
</div>
         </div>

1 个答案:

答案 0 :(得分:0)

很奇怪,您定义了$ goalsresult来存储来自mysql_query调用的结果集,并认为您希望传递该结果集而不是$ result-除非$ result是您在其他地方定义并有意使用的东西。

以下是您的代码示例:

$goalsresult = mysqli_query($conn,$topgoals) or die(mysqli_error($conn));

if(mysqli_num_rows($goalsresult)>0) {
     while($row = mysqli_fetch_assoc($goalsresult)){

希望这能满足您的需求。