Jump to content

[SOLVED] Can you query MYSQL and do a "while" function while inside another "while&qu


blurredvision

Recommended Posts

I know, the title confused me too :).  But, I have a query that I use in a "while" statement.  This is basically bringing back a team, the user's online name, then their opponent.  I'm wanting to check to see if their opponent is another active user in the league, and if so, then attach that user's online name to the opponent team name.  Obviously, this is not working, and I'm not even sure if I can do what I'm trying to do.

 

I've attached my code.  Is it possible to set up a new query and "while" function inside of a currently running "while" function, or does this break the database link to the first "while" function that is running?  As you'll see, I'm even attempting to use the $matchups array within the internal "while" loop alongside the $check array, but I'm not sure if this would even work.

 

$currentgamesquery = "SELECT * FROM schedules, user WHERE schedules.gamertag_id = user.gamertag_id AND schedules.season=$currentseason AND schedules.week=$currentweek";
		$cgresult = @mysqli_query($dbc, $currentgamesquery);

		while($matchups = mysqli_fetch_array($cgresult)) {
			if ($matchups['opponent'] != 'week' . $currentweek . 'bye') { // Check to see if they have a bye week.
				if ($matchups['home_away'] == 1) { // If it's a home game, use the * vs * monikor.
					echo '<span class="enterstats">';
					echo $matchups['team'] . ' (' . $matchups['gamertag'] . ') vs ' . $matchups['opponent'];

                                                                             // Query the database again to check against current teams listed in the database that have users assigned to them in order to append their online name to the team name.
					$query = "SELECT team,gamertag FROM user";
					$queryresult = @mysqli_query($dbc, $query);

					while($check = mysqli_fetch_array($queryresult) {
						if ($matchups['opponent'] == $check['team']) {
							echo ' (' . $check['gamertag'] . ')';
						}
					}

					echo '</span><br />';
				} else { // If it's not a home game, then it has to be an away game, so use the * at * monikor, and repeat everything from above.
					echo '<span class="enterstats">';
					echo $matchups['team'] . ' (' . $matchups['gamertag'] . ') at ' . $matchups['opponent'];

					$query = "SELECT opponent,team FROM user";
					$queryresult = @mysqli_query($dbc, $query);

					while($check = mysqli_fetch_array($queryresult) {
						if ($matchups['opponent'] == $check['team']) {
							echo ' (' . $check['gamertag'] . ')';
						}
					}

					echo '</span><br />';
				}
			}	
		}

You can have a while inside a while, just make sure its not an infinite loop.

 

Well maybe that's what I'm running into then, because when I try to run this, I don't get any errors, just a blank white page, and it take a while for my page to be able to load again when I comment out that internal "while" function.  But is it even possible to get stuck in an infinite loop when you're only retreiving finite rows from a database?

i wouldn't worry about an infinite loop.  what i would worry about is that you've shut off the most handy debugging tools: errors.  drop the @ in front of the nested query and add the or die() clause.  could just be a syntax issue, or maybe they'll confirm your concern about nested queries breaking your connection.  i've never had issues with nested queries except variable replacement, but i haven't used the mysqli extension.

as an added bonus, you have a syntax error here:

 

while($check = mysqli_fetch_array($queryresult) {

 

i can haz closing parenthesis?

 

No, but you can haz cheezburgur. :D  And I think you can just do a join to get the opponent's name.

as an added bonus, you have a syntax error here:

 

while($check = mysqli_fetch_array($queryresult) {

 

i can haz closing parenthesis?

 

That must've been it, because now it's working beautifully!  Thanks.

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.