Jump to content

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 />";
      	}
 	?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.