简体   繁体   English

从mysql显示记录/表。 问题

[英]Display records/table from mysql. Issue

I want display data from mysql on my php page . 我想在php页面上显示来自mysql的数据。 Everything works well but... 一切都很好,但是...

I have two tables . 我有两张桌子。 Players and Combat . 玩家与战斗。

On players I gave username,unique_id 在我给玩家的用户名上,unique_id

On combat I have unique_id , kills , deaths . 在战斗中,我有unique_id,杀死人数,死亡人数。

I want split connect these two tables and display them using this code/table 我想拆分连接这两个表并使用此代码/表显示它们

在此处输入图片说明

My code 我的密码

 <?php  
 $connect = mysqli_connect("localhost", "root", "password", "dbname");  
 $query ="SELECT * FROM player ORDER BY unique_id DESC";  
 $result = mysqli_query($connect, $query);  
 ?>  
 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>Player Data</title>  
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
           <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>  
           <script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>            
           <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" />  
      </head>  
      <body>  
           <br /><br />  
           <div class="container">  
                <h3 align="center"> Player Data</h3>  
                <br />  
                <div class="table-responsive">  
                     <table id="employee_data" class="table table-striped table-bordered">  
                          <thead>  
                               <tr>  
                                    <td>username</td>  
                                    <td>unique_id</td>  

                               </tr>  
                          </thead>  
                          <?php  
                          while($row = mysqli_fetch_array($result))  
                          {  
                               echo '  
                               <tr>  
                                    <td>'.$row["username"].'</td>  
                                    <td>'.$row["unique_id"].'</td>
                               </tr>  
                               ';  
                          }  
                          ?>  
                     </table>  
                </div>  
           </div>  
      </body>  
 </html>  
 <script>  
 $(document).ready(function(){  
      $('#employee_data').DataTable();  
 });  
 </script>

You can use INNER JOIN in SQL to connect two tables on a shared value. 您可以在SQL中使用INNER JOIN在一个共享值上连接两个表。

I'm assuming that the value in unique_id is the column both tables have in common. 我假设unique_id中的值是两个表共有的列。

If you change your database query to: 如果您将数据库查询更改为:

SELECT 
    player.unique_id,  
    player.username,
    combat.kills,
    combat.deaths
FROM 
    player
INNER JOIN 
    combat ON player.unique_id = combat.unique_id
ORDER BY 
    player.unique_id DESC

This query will join both tables where the columns unique_id has the same value. 此查询将unique_id两个表,其中unique_id列的值相同。

Now you can echo the content just like before: 现在,您可以像以前一样回显内容:

<td>'.$row["username"].'</td>  
<td>'.$row["unique_id"].'</td>
<td>'.$row["kills"].'</td>
<td>'.$row["deaths"].'</td>

You can read more about INNER JOIN here: http://www.mysqltutorial.org/mysql-inner-join.aspx 您可以在这里阅读有关INNER JOIN更多信息: http : //www.mysqltutorial.org/mysql-inner-join.aspx

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

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