lukep11a Posted July 3, 2011 Share Posted July 3, 2011 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 Link to comment https://forums.phpfreaks.com/topic/241000-help-displaying-data-from-multiple-tables/ Share on other sites More sharing options...
wildteen88 Posted July 3, 2011 Share Posted July 3, 2011 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. Link to comment https://forums.phpfreaks.com/topic/241000-help-displaying-data-from-multiple-tables/#findComment-1237906 Share on other sites More sharing options...
lukep11a Posted July 3, 2011 Author Share Posted July 3, 2011 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 />"; } ?> Link to comment https://forums.phpfreaks.com/topic/241000-help-displaying-data-from-multiple-tables/#findComment-1237910 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.