Firstly apologies if this has been posted elsewhere. I've spent a considerable amount of time trying to find an answer to this and seem to be getting nowhere!
I wonder if anyone can help me to do what I need to do. Basically I have a table that stores the fixtures of games to be played. The fixtures are stored with the by the IDs of the team from the teamTable (teamID). Obviously as there are two teams in one game of football I need to have the teamID put in twice (column names; teamA, teamB). This is stored correctly. I then want to extract the results from the table but for the viewer the teamIDs mean nothing, I therefore need to extract the team names linked to the teamIDs in the team table.
Below is the code I have so far;
<?php
// Make a MySQL Connection
mysql_connect("localhost", "username", "pwd") or die(mysql_error());
mysql_select_db("lsl") or die(mysql_error());
// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM fixture")
or die(mysql_error());
//$teamNameA = mysql_query("SELECT teamName FROM fixture, team WHERE team.teamID = fixture.teamA")or die(mysql_error());
//$teamNameB = mysql_query("SELECT teamName FROM fixture, team WHERE team.teamID = fixture.teamB")or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Team 1</th> <th>Team 2</th> <th>Year</th><th>Venue</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result ) ) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['teamA'];
echo "</td><td>";
echo $row['teamB'];
echo "</td>";
echo "<td>";
echo $row['fixtureYear'];
echo "</td><td>";
echo $row['fixtureVenue'];
echo "</td></tr>";
}
echo "</table>";
?>
This returns the information from the fixtures table and the correct teams, venue and year, however it doesn't display the team name, which is what I need it to do. I've tried many different ways of restructuring the SELECT query such as including the team table however, I don't ever seem to get a working product. Also, it's important to stress teamA and teamB are just the teamIDs. There is foreign key set up for both of these columns and in a previous attempted query I have has the correct team names showing (although nothing in the right order) which shows that the tables are linked correctly and the foreign key constraints are working. (There are two foreign key constraints as there are two columns that require the teamID)
So, if anyone could help me to restructure what I have so that the teamNames are showing instead of the IDs I would be much appreciative, and I presume it'll be to do with my query. Once the query is sorted I'll then be able to change the fields it is printing out from teamA/teamB to teamName in both (which is the name of the column in the team table with the team name in).
I am a bit new to this level of PHP but if anyone can help me I'd be much appreciative!
Thanks in advance,
Aidan