I've created a small database based on the Olympics for running a few sql queries from. I am trying to generate a table that displays each Country name and the total of Gold, Silver & Bronze medals won based on Athletes that are from these countries. Here is a screenshot of my Relational Model View:
Here is my query I am trying to run:
<h1>Olympics Database</h1>
<h3>Summary Information</h3>
<h4>Number of Olympic Athletes from United Kingdom</h4>
<?php
$conn = mysqli_connect('localhost', '#####', '#####') or die ('Could not connect:' . mysqli_error($conn));
echo 'Successfully Connected. <br/>';
mysqli_select_db($conn, '#####') or die('Database will not open');
echo 'Database Connected. <br/> <br/>';
$query2 = 'SELECT CountryAbbrev, (SELECT COUNT(CountryAbbrev) FROM athlete WHERE MedalID = 401 AND ath.CountryAbbrev = athlete.CountryAbbrev) AS Gold, (SELECT COUNT(CountryAbbrev) FROM athlete WHERE MedalID = 402 AND ath.CountryAbbrev = athlete.CountryAbbrev) AS Silver, (SELECT COUNT(CountryAbbrev) FROM athlete WHERE MedalID = 403 AND ath.CountryAbbrev = athlete.CountryAbbrev) AS Bronze FROM athlete AS ath';
$result2 = mysqli_query($conn, $query2) or die ('Invalid Query');
echo'<table><tr><th>Country Name</th><th>Gold</th><th>Silver</th><th>Bronze</th></tr>';
$row = mysqli_fetch_row($result2);
echo'<tr><td>' .$row[0].'</td><td>' .$row[1].'</td><td>' .$row[2].'</td><td>' .$row[3].'</td></tr>';
echo'</table>';
mysqli_close($conn);
?>
All I'm getting is invalid query. Going by my relation model view, could anyone help me figure out how I can display the total of gold, silver and bronze medals won by each country in a single table?


SUM(MedalID)will sum ID values not count them.or dieyou really want to be looking atmysqli_errorto see what the actual problem is rather than guessing.