Jump to content

Table Join query


lukep11a

Recommended Posts

Hi, I have a fantasy football website, and on a user account page I want to display fixtures that are coming up that include teams that the current user has chosen. My test_teams table stores all the team names and their teamid. The test_selections table is where each users team selections are stored, it has two columns, userid and teamid. The test_fixtures table has two columns, hometeam and awayteam, these two cloumns hold the teamid of the teams that are playing.

 

The code below correctly displays the fixtures that contain any of the current users team selections. However, it is only displaying the teamid of the teams that are playing as they have not been matched to the test_teams table to get the team name. Does anybody now how I can do this? I believe it can be done using a left join but so far I just keep getting errors when i try to write the code. Any help would be very much appreciated.

 

<table width="380" border="0">
	<?php
	$query = "SELECT test_fixtures.competition, test_fixtures.date, test_fixtures.hometeam, test_fixtures.awayteam FROM test_fixtures, test_selections WHERE test_selections.userid = '{$_SESSION['userid']}' AND (test_selections.teamid = test_fixtures.hometeam OR test_selections.teamid = test_fixtures.awayteam)";
        $result = mysql_query($query) or die(mysql_error());
        
        while($row = mysql_fetch_assoc($result))
	{
	?>
	<tr>
	<td width="85" class="fixtures_date"><?php echo $row['date']; ?></td>
	<td width="30" class="fixtures_comp"><?php echo $row['competition']; ?></td>
	<td width="135" class="fixtures_home_teams"><?php echo $row['hometeam']; ?></td>
	<td width="25" class="fixtures_center">v</td>
	<td width="135" class="fixtures_away_teams"><?php echo $row['awayteam']; ?></td>
	</tr>
	<?php
	}
	?>
	</table>

Link to comment
https://forums.phpfreaks.com/topic/245298-table-join-query/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.