Jump to content

While Loop in While Loop


ueon

Recommended Posts

The individual while loops work, but when I embed it within a while the parent loop runs, but the inner loop doesn't execute at all

 

	
while ($set3 = mysql_fetch_array($query2)){
	while ($set2 = mysql_fetch_array($query)){
		echo $set2['EmailAddress'];
	}
}

Link to comment
Share on other sites

There's nothing inherently flawed with nested while loops, so the problem is with your code, but you're not giving us enough context to see what the issue is. Try posting several lines above that.

Link to comment
Share on other sites

I can identify an issue with your logic already, as:

 

while ($set2 = mysql_fetch_array($query))

 

mysql_fetch_array - Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.

 

"moves the internal data pointer ahead" This means once the pointer moves to the end, it will not be looped again unless you reset the pointer like so:

 

mysql_data_seek($query);

 

I would like to see the rest of your code, though, as it looks like you may have more issues.

Link to comment
Share on other sites

I can identify an issue with your logic already, as:

 

while ($set2 = mysql_fetch_array($query))

 

mysql_fetch_array - Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.

 

"moves the internal data pointer ahead" This means once the pointer moves to the end, it will not be looped again unless you reset the pointer like so:

 

mysql_data_seek($query);

 

I would like to see the rest of your code, though, as it looks like you may have more issues.

 

The OP doesn't want to loop through the same results multiple times, and the loops use different MySQL results, so that shouldn't be an issue. It will be looped again if there are multiple results.

Link to comment
Share on other sites

Essentially his logic would look like this:

 

Loop1 Output

  Loop2 Output

  Loop2 Output

  Loop2 Output

  Loop2 Output

 

Loop1 Output

Loop1 Output

Loop1 Output

Loop1 Output

 

Loop2 would execute once and then never do anything again. But as we have both said, we need the rest of the code to understand what you're doing.

Link to comment
Share on other sites

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.