473,657 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($qu ery_1) or die(mysql_error ());

$query_2 = "SELECT field_2 FROM table_2";
$result_2 = mysql_query($qu ery_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 7299
"Paul Charlton-Thomson" <pa************ *****@hotmail.c om> wrote in message
news:41******** *************** @ptn-nntp-reader02.plus.n et...
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($qu ery_1) or die(mysql_error ());

$query_2 = "SELECT field_2 FROM table_2";
$result_2 = mysql_query($qu ery_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**********@c omcast.net> wrote in message
news:zDn4d.5328 $He1.3955@attbi _s01...
"Paul Charlton-Thomson" <pa************ *****@hotmail.c om> wrote in message
news:41******** *************** @ptn-nntp-reader02.plus.n et...
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($qu ery_1) or die(mysql_error ());

$query_2 = "SELECT field_2 FROM table_2";
$result_2 = mysql_query($qu ery_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.co m 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($qu ery_1) or die(mysql_error ());

$query_2 = "SELECT field_2 FROM table_2";
$result_2 = mysql_query($qu ery_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($qu ery_1) or die(mysql_error ());
while ($row_1 = mysql_fetch_row ($result_1))
{
$result_2 = mysql_query($qu ery_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.sp am.scu.edu.au> wrote in message
news:MP******** *************** *@news.asgard.n et.au...
pa************* ****@hotmail.co m 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($qu ery_1) or die(mysql_error ());

$query_2 = "SELECT field_2 FROM table_2";
$result_2 = mysql_query($qu ery_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($qu ery_1) or die(mysql_error ());
while ($row_1 = mysql_fetch_row ($result_1))
{
$result_2 = mysql_query($qu ery_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
12696
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 increase. Does Python have a limit on the number of nested for-loops? Thanks.
0
1784
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 dict() print combo2(5)
22
2071
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 a loop that has someone guess at a number 5 times and if they get 45, it tells them how many guesses they needed. It also has to tell them if they leave the range of guesses from 0 to 100. I need help. Can somebody help me please!!!
46
9911
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 already in a loop." Then he goes on to portray a contrived example that doesn't tell me under what conditions a nested loop might be favoured as a solution? i.e. what are nested loops useful for? What kinds of algorithms are served by nested loops?...
77
5195
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. Is this because .NET is inherently slower or does the C# compiler merely produce code that is not as well optimized as the C++ compiler?
9
2839
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
3116
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 trying to understand Goto in this contect. PS: This is not homework.
13
2687
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
7247
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 <Boolean ExpressionThen Exit For Next If Not <Boolean ExpressionThen Exit For
0
8413
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8842
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7352
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.