简体   繁体   English

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

[英]How to display each row from sql in a table in php

I have a table within a modal.我在模态中有一个表。 I want the table to display the top 3 goalscorers from a football game.我希望表格显示足球比赛的前 3 名射手。 The following code is not returning the table.以下代码不返回表。 Any help greatly appreciated!.非常感谢任何帮助! At present I am trying to echo the table within the php tags but I am not sure if this is the best way to do this I took this approach as I am expecting three rows to be returned from the database.目前我正在尝试在 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>

Noticed you define $goalsresult to store the result set from your mysql_query call and think you'd want to pass that instead of $result - unless $result is something you have defined elsewhere and are using intentionally.注意到您定义了 $goalsresult 来存储来自您的 mysql_query 调用的结果集,并认为您希望传递它而不是 $result - 除非 $result 是您在其他地方定义并有意使用的内容。

Here's an example with your code:这是您的代码示例:

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

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

Hope this gets you what you need.希望这能让你得到你所需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM