No Data found Query failed in mysql

Advertisements

I have to fetch data from 3 tables that is (players, tournaments,entries, and groups) and show that data on the web page but I am not getting any of the data on the table when I use join it shows nothing while I used just a single select * from entries to get the fees data but not getting the other data when i write query using join :

i want to show the below data to the output :

Output that i want to show on page


<?php
include_once('assets/config.php');
include_once('functions.php'); 


 

// Check the connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Retrieve data from the database
$sql = "SELECT tournaments.tour_name, entries.fees, players.f_name, groups.group_name
FROM entries
JOIN tournaments ON entries.tour_id = tournaments.tour_id
JOIN players ON entries.player_id = players.player_id
JOIN groups ON entries.group_id = groups.group_id;
";





$result = mysqli_query( $conn, $sql )or die( mysqli_error( $conn ) );




// Create the HTML table with border and styling
echo "<table style='border-collapse: collapse; width: 100%;'>";
echo "<thead style='background-color: #ddd;'><tr><th style='padding: 8px; border: 1px solid #ddd;'>Player</th><th style='padding: 8px; border: 1px solid #ddd;'>Tournaments</th><th style='padding: 8px; border: 1px solid #ddd;'>Group</th><th style='padding: 8px; border: 1px solid #ddd;'>Fees</th></tr></thead>";
echo "<tbody>";

if(mysqli_num_rows($result) > 0) {  
  while($row = mysqli_fetch_array($result)) {


      echo "<tr>";
      echo "<td style='padding: 8px; border: 1px solid #ddd;'>" . $row["f_name"] . "</td>";
      echo "<td style='padding: 8px; border: 1px solid #ddd;'>" . $row["tour_name"] . "</td>";
      echo "<td style='padding: 8px; border: 1px solid #ddd;'>" . $row["group_name"] . "</td>";
      echo "<td style='padding: 8px; border: 1px solid #ddd;'>" . $row["fees"]. "</td>";
      echo "</tr>";
  }
} else {
  echo "<tr><td colspan='4' style='padding: 8px; border: 1px solid #ddd;'>No data found.</td></tr>";
  printf("Query failed: %s\n", $conn->error);
}

echo "</tbody></table>";




// Close the database connection
$conn->close();

?>

Current output

How can i query it perfectly show the ouput

>Solution :

SELECT entries.id, players.f_name AS Player, tournaments.tour_name AS Tournoment, groups.group_name AS Groups, entries.fees
FROM entries
INNER JOIN players ON entries.player_id = players.id
INNER JOIN tournaments ON entries.tournament_id = tournaments.id
INNER JOIN groups ON entries.group_id = groups.id;

This query selects the id and fees columns from the entries table, the f_name column from the players table, the tour_name column from the tournaments table, and the group_name column from the groups table. It then performs an inner join on each table to join the relevant rows.

Here is some example data that you can use to test the query:

-- Players table
id  | f_name
----|-------
1   | John
2   | Jane

-- Tournaments table
id  | tour_name
----|----------
1   | Tournament A
2   | Tournament B

-- Groups table
id  | group_name
----|-----------
1   | Group A
2   | Group B

-- Entries table
id  | player_id | tournament_id | group_id | fees
----|-----------|--------------|---------|-----
1   | 1         | 1            | 1       | 100
2   | 2         | 2            | 2       | 150

When you run the query with this data, the result should look like this:

id | Player | Tournoment   | Groups | fees
---|--------|--------------|--------|-----
1  | John   | Tournament A | Group A | 100
2  | Jane   | Tournament B | Group B | 150

You can then use this result in your application to display the relevant data to the user.

i can help you better if you define your database structure, and give me feedback of this answer.

Leave a ReplyCancel reply