473,405 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Struggling with nested while loops

Hi,

I am trying to execute 2 queries and then output the results in 2 loops but
maybe I'm using the wrong approach.

Can anyone help me here ...

$query_1 = "SELECT field_1 FROM table_1";
$result_1 = mysql_query($query_1) or die(mysql_error());

$query_2 = "SELECT field_2 FROM table_2";
$result_2 = mysql_query($query_2) or die(mysql_error());

while ($row_1 = mysql_fetch_row($result_1))
{
while ($row_2 = mysql_fetch_row($result_2))
{
print $row_1[0] . ' - ' . $row_2[0] . '<BR>';
}
}
Thanks in advance.

Paul.
Jul 17 '05 #1
4 7288
"Paul Charlton-Thomson" <pa*****************@hotmail.com> wrote in message
news:41***********************@ptn-nntp-reader02.plus.net...
Hi,

I am trying to execute 2 queries and then output the results in 2 loops but maybe I'm using the wrong approach.

Can anyone help me here ...

$query_1 = "SELECT field_1 FROM table_1";
$result_1 = mysql_query($query_1) or die(mysql_error());

$query_2 = "SELECT field_2 FROM table_2";
$result_2 = mysql_query($query_2) or die(mysql_error());

while ($row_1 = mysql_fetch_row($result_1))
{
while ($row_2 = mysql_fetch_row($result_2))
{
print $row_1[0] . ' - ' . $row_2[0] . '<BR>';
}
}
Thanks in advance.

Paul.


mysql_fetch_row($result_2) will run out of rows to return after running
through it the first time. When the nested while loop is finished, and the
next iteration of the outside while loop is run, the nested
mysql_fetch_row() returns FALSE because it has already run through all of
the queried rows.

- JP
Jul 17 '05 #2
OK I understand that ... is there a way round that? Do I have to read all
the results from the first query into an array then run the array inside a
while loop?

"kingofkolt" <je**********@comcast.net> wrote in message
news:zDn4d.5328$He1.3955@attbi_s01...
"Paul Charlton-Thomson" <pa*****************@hotmail.com> wrote in message
news:41***********************@ptn-nntp-reader02.plus.net...
Hi,

I am trying to execute 2 queries and then output the results in 2 loops

but
maybe I'm using the wrong approach.

Can anyone help me here ...

$query_1 = "SELECT field_1 FROM table_1";
$result_1 = mysql_query($query_1) or die(mysql_error());

$query_2 = "SELECT field_2 FROM table_2";
$result_2 = mysql_query($query_2) or die(mysql_error());

while ($row_1 = mysql_fetch_row($result_1))
{
while ($row_2 = mysql_fetch_row($result_2))
{
print $row_1[0] . ' - ' . $row_2[0] . '<BR>';
}
}
Thanks in advance.

Paul.


mysql_fetch_row($result_2) will run out of rows to return after running
through it the first time. When the nested while loop is finished, and the
next iteration of the outside while loop is run, the nested
mysql_fetch_row() returns FALSE because it has already run through all of
the queried rows.

- JP

Jul 17 '05 #3
pa*****************@hotmail.com says...
Hi,

I am trying to execute 2 queries and then output the results in 2 loops but
maybe I'm using the wrong approach.

Can anyone help me here ...

$query_1 = "SELECT field_1 FROM table_1";
$result_1 = mysql_query($query_1) or die(mysql_error());

$query_2 = "SELECT field_2 FROM table_2";
$result_2 = mysql_query($query_2) or die(mysql_error());

while ($row_1 = mysql_fetch_row($result_1))
{
while ($row_2 = mysql_fetch_row($result_2))
{
print $row_1[0] . ' - ' . $row_2[0] . '<BR>';
}
}


What exactly are you trying to do?

Print a complete list of the values in $query_2 for each and every value
in $query_1? If so, why not just do a single query with a Cartesian (no
join) state:
$query = "SELECT field_1, field_2 FROM table_1, table_2";

Alternatively, you can get that result by:
$query_1 = "SELECT field_1 FROM table_1";
$query_2 = "SELECT field_2 FROM table_2";

$result_1 = mysql_query($query_1) or die(mysql_error());
while ($row_1 = mysql_fetch_row($result_1))
{
$result_2 = mysql_query($query_2) or die(mysql_error());
while ($row_2 = mysql_fetch_row($result_2))
{
print $row_1[0] . ' - ' . $row_2[0] . '<BR>';
}
}

But why are you looping through two tables that appear to have no
relationship?

Geoff M
Jul 17 '05 #4
That works ... thanks ;-)

Paul.

"Geoff Muldoon" <gm******@no.spam.scu.edu.au> wrote in message
news:MP************************@news.asgard.net.au ...
pa*****************@hotmail.com says...
Hi,

I am trying to execute 2 queries and then output the results in 2 loops but maybe I'm using the wrong approach.

Can anyone help me here ...

$query_1 = "SELECT field_1 FROM table_1";
$result_1 = mysql_query($query_1) or die(mysql_error());

$query_2 = "SELECT field_2 FROM table_2";
$result_2 = mysql_query($query_2) or die(mysql_error());

while ($row_1 = mysql_fetch_row($result_1))
{
while ($row_2 = mysql_fetch_row($result_2))
{
print $row_1[0] . ' - ' . $row_2[0] . '<BR>';
}
}


What exactly are you trying to do?

Print a complete list of the values in $query_2 for each and every value
in $query_1? If so, why not just do a single query with a Cartesian (no
join) state:
$query = "SELECT field_1, field_2 FROM table_1, table_2";

Alternatively, you can get that result by:
$query_1 = "SELECT field_1 FROM table_1";
$query_2 = "SELECT field_2 FROM table_2";

$result_1 = mysql_query($query_1) or die(mysql_error());
while ($row_1 = mysql_fetch_row($result_1))
{
$result_2 = mysql_query($query_2) or die(mysql_error());
while ($row_2 = mysql_fetch_row($result_2))
{
print $row_1[0] . ' - ' . $row_2[0] . '<BR>';
}
}

But why are you looping through two tables that appear to have no
relationship?

Geoff M

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
0
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # David Eppstein of the Geometry Junkyard fame gave this elegant # version for returing all possible pairs from a range of n numbers. def combo2(n): return...
22
by: Stan | last post by:
Hey everyone, I've got a computer science class and we're working on C++. I am struggling with nested loops and have a really simple assignment, but I can't get it to run the loop. I need to run...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
9
by: Gregory Petrosyan | last post by:
I often make helper functions nested, like this: def f(): def helper(): ... ... is it a good practice or not? What about performance of such constructs?
5
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
Could someone give me a simple example of nested scope in C#, please? I've searched Google for this but have not come up with anything that makes it clear. I am looking at the ECMA guide and...
13
by: Fredrik Lundh | last post by:
Patrol Sun wrote: so why exactly are you trying to nest 20 or 100 for-in loops? </F>
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.