xwishmasterx Posted May 8, 2011 Share Posted May 8, 2011 I am wondering if it is possible to create 2 queries, and use 1 while statement? Something like: while (($row = mysql_fetch_array($rs_getevent)) && ($row = mysql_fetch_array($rs_getevent2))) { Is something like this possible? Quote Link to comment https://forums.phpfreaks.com/topic/235832-2-queries-inside-same-while-statement/ Share on other sites More sharing options...
xyph Posted May 8, 2011 Share Posted May 8, 2011 Why not test it yourself? Quote Link to comment https://forums.phpfreaks.com/topic/235832-2-queries-inside-same-while-statement/#findComment-1212266 Share on other sites More sharing options...
xwishmasterx Posted May 8, 2011 Author Share Posted May 8, 2011 I have tried that code, that didn't work, but it might be possible why I ma asking. The code is just to show what I am trying to achieve. Quote Link to comment https://forums.phpfreaks.com/topic/235832-2-queries-inside-same-while-statement/#findComment-1212268 Share on other sites More sharing options...
spiderwell Posted May 8, 2011 Share Posted May 8, 2011 in short it wont work as the second dataset will replace the first as you pass it to the same variable. are you able to use a JOIN statement in your sql to get it all in one go? Quote Link to comment https://forums.phpfreaks.com/topic/235832-2-queries-inside-same-while-statement/#findComment-1212269 Share on other sites More sharing options...
xyph Posted May 8, 2011 Share Posted May 8, 2011 And if a JOIN isn't possible, just use two loops. One loop doing twice the work as two won't save you much processing power. Just a little bit of code. This snippet from the php.net website even dumps the result into an array in a single line. Daniel Chcouri - 333222 +A-T+ gmail 02-Apr-2009 11:45 Fetching all the results to array with one liner: <?php $result = mysql_query(...); while(($resultArray[] = mysql_fetch_assoc($result)) || array_pop($resultArray)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/235832-2-queries-inside-same-while-statement/#findComment-1212273 Share on other sites More sharing options...
spiderwell Posted May 8, 2011 Share Posted May 8, 2011 nice i didn't know you could do that! Quote Link to comment https://forums.phpfreaks.com/topic/235832-2-queries-inside-same-while-statement/#findComment-1212275 Share on other sites More sharing options...
xwishmasterx Posted May 8, 2011 Author Share Posted May 8, 2011 Thanks for the suggestions. As being fairly new to this I'm getting more and more confused lol. I'll see if I can make some of this work, otherwise I'll reply with the code I have so it is more clear what I am trying to achieve (there's usually a simple answer to my questions) Quote Link to comment https://forums.phpfreaks.com/topic/235832-2-queries-inside-same-while-statement/#findComment-1212276 Share on other sites More sharing options...
xyph Posted May 8, 2011 Share Posted May 8, 2011 Good luck. Feel free to ask about specific parts of code that you don't understand. If the PHP manual doesn't explain it well, volunteers like us will try to explain it in a different way for you Quote Link to comment https://forums.phpfreaks.com/topic/235832-2-queries-inside-same-while-statement/#findComment-1212280 Share on other sites More sharing options...
xwishmasterx Posted May 8, 2011 Author Share Posted May 8, 2011 After a look at this, I simply have no idea what to do with it So here are some more info; I am trying to list some events by date. My problem is listing 2 events happening the same day where a "variable" can be 2 things. As picture above shows team with id "21" is both attacker and defender on same date. (disregards war_ended date) When showing the events, It should show the date, the defending team name if any, and the attacking team if any. my query right now is: $sql_getevent = "SELECT DATE(war_started) AS war_started, attacker_name, defender_name, won, lost FROM teamwar_info WHERE attacker=".$_GET['t']." ORDER BY DATE(war_started) DESC "; $rs_getevent = mysql_query($sql_getevent); This will show the date the right team name of the team being attacked. The next column should show the team name of the team ATTACKING (on picture it should be "Black Beards Crew" as this team is attacking the team with id 21. Using above picture: Assume our team has id 21. The event list should show date: 2011-5-10, attacking (the team WE attack): Jack Sparrow, the team attacking US: Black Beards Crew. How can I do this? (I am sure this is confusing, so please ask and I'll try to further explain.) Quote Link to comment https://forums.phpfreaks.com/topic/235832-2-queries-inside-same-while-statement/#findComment-1212283 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.