JJohnsenDK Posted December 30, 2006 Share Posted December 30, 2006 HeyIs it possible to loop through two mysql_fetch_array funtions?In my example i want the while loop to loop through both $fet1 and $fet2, is this somehow possible?I tried with [i]while ($fet2 = mysql_fetch_array($que2) && $fet2 = mysql_fetch_array($que2)) [/i] but this only result in that it looped through the last variable $fet2.There most be anohter way?[code]<?phpinclude('config.php');$sql1 = "SELECT team_id, name, home_id FROM team, game WHERE team_id = home_id";$sql2 = "SELECT team_id, name, visitor_id FROM team, game WHERE team_id = visitor_id";$que1 = mysql_query($sql1);$que2 = mysql_query($sql2); $fet1 = mysql_fetch_array($que1);while ($fet2 = mysql_fetch_array($que2)){ echo $fet1['name']." - ".$fet2['name']."<br>";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/32285-solved-is-it-possible-to-loop-through-two-mysql_fetch_array-funtions/ Share on other sites More sharing options...
Psycho Posted December 30, 2006 Share Posted December 30, 2006 [quote author=JJohnsenDK link=topic=120386.msg493681#msg493681 date=1167492963]I tried with [i]while ($fet2 = mysql_fetch_array($que2) && $fet2 = mysql_fetch_array($que2)) [/i] but this only result in that it looped through the last variable $fet2.[/quote]Well those two statemetns are EXACTLY the same. Didn't you mean to include [b]$fet1 = mysql_fetch_array($que1)[/b] as one of the operands? The way you have it written I would expect that you would only get every other value from the second result set.I see no reason that wouldn't work if it was properly written for both result sets. However, what exactly are you trying to do, I think a better approach would be to create a single query to return all the data you need.[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/32285-solved-is-it-possible-to-loop-through-two-mysql_fetch_array-funtions/#findComment-149859 Share on other sites More sharing options...
wildteen88 Posted December 30, 2006 Share Posted December 30, 2006 Post the databases schema for team and game tables here. And I'll be able to join those two queries into one for you. No need for two queries. Quote Link to comment https://forums.phpfreaks.com/topic/32285-solved-is-it-possible-to-loop-through-two-mysql_fetch_array-funtions/#findComment-149862 Share on other sites More sharing options...
JJohnsenDK Posted December 30, 2006 Author Share Posted December 30, 2006 Teams-------team_idnamecoachGames-------game_idhome_idvisitor_id Quote Link to comment https://forums.phpfreaks.com/topic/32285-solved-is-it-possible-to-loop-through-two-mysql_fetch_array-funtions/#findComment-149868 Share on other sites More sharing options...
wildteen88 Posted December 30, 2006 Share Posted December 30, 2006 Try the following query out:[code]$sql = "SELECT t.team_id, t. name, g.home_id, g.visitor_id FROM team AS t, game AS g WHERE t.team_id = g.home_id AND t.team_id = g.visitor_id";[/code]That should do it. You might want to have a read about SQL Joins. That is what I turned your query in to. Quote Link to comment https://forums.phpfreaks.com/topic/32285-solved-is-it-possible-to-loop-through-two-mysql_fetch_array-funtions/#findComment-149874 Share on other sites More sharing options...
JJohnsenDK Posted December 30, 2006 Author Share Posted December 30, 2006 I actually tried that before, but first how do i show the team name(the colum name from table team)? and second how do i show the visitor name and home name seperatly?[code]<?phpinclude('config.php');$sql = "SELECT t.team_id, t. name, g.home_id, g.visitor_id FROM team AS t, game AS g WHERE t.team_id = g.home_id AND t.team_id = g.visitor_id";$que = mysql_query($sql);while ($fet = mysql_fetch_array($que)){ echo $fet['t.name']." - ".$fet['t.name']."<br>";}?>[/code]This doesnt work. Quote Link to comment https://forums.phpfreaks.com/topic/32285-solved-is-it-possible-to-loop-through-two-mysql_fetch_array-funtions/#findComment-149884 Share on other sites More sharing options...
wildteen88 Posted December 30, 2006 Share Posted December 30, 2006 Your while loop is wrong. What columns hold the team name and visitor name in your teams and games table name is the team name and you. so you use $fet['name'] to get the team name. (note the t. and g. in the query are aliases - you don't reference them in your code. Just the actual column name).So what coloumn holds the visitor name? Quote Link to comment https://forums.phpfreaks.com/topic/32285-solved-is-it-possible-to-loop-through-two-mysql_fetch_array-funtions/#findComment-149891 Share on other sites More sharing options...
JJohnsenDK Posted December 31, 2006 Author Share Posted December 31, 2006 The database is design in this way:Game holds only numbers. game_id = the number of the match, home_id = the number of the home team in the team table and visitor = the number of the visitor team in the team table.For example the first game of the season looks like this:Game table:game_id = 1, home_id=3 and visitor_id=1 Team table:team_id = 1 - name = Silkeborgteam_id = 2 - name = Brøndbyteam_id = 3 - name = Fc KøbenhavnThen the first game of the season should look like this when it is printed to the website:Match nr. 1 - Fc København - SilkeborgI hope you understand my example, else plz do write so. Quote Link to comment https://forums.phpfreaks.com/topic/32285-solved-is-it-possible-to-loop-through-two-mysql_fetch_array-funtions/#findComment-150315 Share on other sites More sharing options...
Psycho Posted December 31, 2006 Share Posted December 31, 2006 I think this will get you what you want:[code]<?phpinclude('config.php');$sql = "SELECT h.name as home, v.name AS visitor FROM game AS g LEFT JOIN team AS h ON g.home_id = h.team_id LEFT JOIN team AS v ON g.visitor_id = v.team_id";$result = mysql_query($sql);if (mysql_num_rows($result)==0) { echo "There were no results";} else { $matchno = 0; while ($row = mysql_fetch_array($result)){ $matchno++; echo "Match nr. {$matchno} - {$row['home']} - {$row['visitor']}<br>"; }}>?[/code] Quote Link to comment https://forums.phpfreaks.com/topic/32285-solved-is-it-possible-to-loop-through-two-mysql_fetch_array-funtions/#findComment-150323 Share on other sites More sharing options...
JJohnsenDK Posted December 31, 2006 Author Share Posted December 31, 2006 That is what i wanted! ;DIm very greatfull for your, thanks alot, both of you! :-* Quote Link to comment https://forums.phpfreaks.com/topic/32285-solved-is-it-possible-to-loop-through-two-mysql_fetch_array-funtions/#findComment-150326 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.