Jump to content

Help displaying data from multiple tables


lukep11a

Recommended Posts

Hi, I have the following code that displays 3 team names picked up from table 'selections':

 

<?php

	$query = "SELECT * FROM selections WHERE username = '{$_SESSION['username']}'";
	$result = mysql_query($query) or die(mysql_error());

	while($row = mysql_fetch_assoc($result))
	{
		echo $row['section1'];
		echo $row['section2'];
		echo $row['section3'];
      	}
 	?>

 

Next to each team name, I want to put the number of points each of the 3 teams currently has, this data is stored in another table called 'teams' under the 'points' column. Is this possible and if so how would I code this in to what I have already? Any help would be very much appreciated.

 

Thanks in advance

Luke

Yes it is possible. You could perform another query within your loop to get the points for each team. However it is not recommended to run queries within a loop.

 

An alternative is to perform one query and grab all the data you'll need. To query multiple tables within one query you can use joins.

Hi, thankyou for your reply, I have got it working by using that tutorial, thanks for that. 1 problem though, it repeats the result 4 times. Any ideas why that could be? This is the code I have now:

 

<?php

	$query = "SELECT * FROM selections, teams WHERE selections.username = '{$_SESSION['username']}'";
	$result = mysql_query($query) or die(mysql_error());

	while($row = mysql_fetch_assoc($result))
	{
		echo $row['section1']. " - ". $row['points'];
		echo "<br />";
		echo $row['section2']. " - ". $row['points'];
		echo "<br />";
		echo $row['section3']. " - ". $row['points'];
		echo "<br />";
      	}
 	?>

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.