Struggling with nested while loops

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Charlton-Thomson

    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.


  • kingofkolt

    #2
    Re: Struggling with nested while loops

    "Paul Charlton-Thomson" <paulcharltonth omson@hotmail.c om> wrote in message
    news:41520382$0 $17932$ed2619ec @ptn-nntp-reader02.plus.n et...[color=blue]
    > Hi,
    >
    > I am trying to execute 2 queries and then output the results in 2 loops[/color]
    but[color=blue]
    > 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.
    >
    >[/color]

    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


    Comment

    • Paul Charlton-Thomson

      #3
      Re: Struggling with nested while loops

      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 " <jessepNOSPAM@c omcast.net> wrote in message
      news:zDn4d.5328 $He1.3955@attbi _s01...[color=blue]
      > "Paul Charlton-Thomson" <paulcharltonth omson@hotmail.c om> wrote in message
      > news:41520382$0 $17932$ed2619ec @ptn-nntp-reader02.plus.n et...[color=green]
      > > Hi,
      > >
      > > I am trying to execute 2 queries and then output the results in 2 loops[/color]
      > but[color=green]
      > > 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.
      > >
      > >[/color]
      >
      > 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
      >
      >[/color]


      Comment

      • Geoff Muldoon

        #4
        Re: Struggling with nested while loops

        paulcharltontho mson@hotmail.co m says...[color=blue]
        > 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>';
        > }
        > }[/color]

        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

        Comment

        • Paul Charlton-Thomson

          #5
          Re: Struggling with nested while loops

          That works ... thanks ;-)

          Paul.



          "Geoff Muldoon" <gmuldoon@no.sp am.scu.edu.au> wrote in message
          news:MPG.1bbcf6 3fa4364a2498968 d@news.asgard.n et.au...[color=blue]
          > paulcharltontho mson@hotmail.co m says...[color=green]
          > > Hi,
          > >
          > > I am trying to execute 2 queries and then output the results in 2 loops[/color][/color]
          but[color=blue][color=green]
          > > 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>';
          > > }
          > > }[/color]
          >
          > 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[/color]


          Comment

          Working...