0

I want display data from mysql on my php page . Everything works well but...

I have two tables . Players and Combat .

On players I gave username,unique_id

On combat I have unique_id , kills , deaths .

I want split connect these two tables and display them using this code/table

enter image description here

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> 
5
  • 2
    Please edit your question to include the code rather than linking to it on an external site. Commented Mar 21, 2018 at 14:25
  • Please edit your question to include an example of the current output and the expected output (together with some example data for your two tables). Currently, it's a bit unclear what you're trying to accomplish and where you're stuck. Commented Mar 21, 2018 at 14:29
  • @vascowhite Done Commented Mar 21, 2018 at 14:32
  • @MagnusEriksson Done . I want add "kills" to my table on my page but data is on another "table" on mysql. Commented Mar 21, 2018 at 14:32
  • @MagnusEriksson can you make it for me? Idk how/where add this . Commented Mar 21, 2018 at 14:52

1 Answer 1

1

You can use INNER JOIN in SQL to connect two tables on a shared value.

I'm assuming that the value in unique_id is the column both tables have in common.

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.

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.