By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,713 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,713 IT Pros & Developers. It's quick & easy.

Struggling with nested while loops

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.