PHP MySQL Single Iteration for data manipulation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Garry

    PHP MySQL Single Iteration for data manipulation

    How do I cycle through a MySQL query result one row at a time so that I can
    do some work on each individual row, instead of having the whole query
    scroll by. I need to have the ability to post each row to a form and then
    work on it, posting the results, then go on to the next row.


    Thank You in Advance for your putting me on the right track.



  • Rik

    #2
    Re: PHP MySQL Single Iteration for data manipulation

    Garry wrote:[color=blue]
    > How do I cycle through a MySQL query result one row at a time so that
    > I can do some work on each individual row, instead of having the
    > whole query scroll by. I need to have the ability to post each row
    > to a form and then work on it, posting the results, then go on to the
    > next row.[/color]

    Euhm, that's already how it works.
    $result = mysql_query('so me query');
    while($row = mysql_fetch_ass oc($result)){
    //do some work on the $row
    }

    Grtz,
    --
    Rik Wasmus


    Comment

    • Garry

      #3
      Re: PHP MySQL Single Iteration for data manipulation

      The problem is that when you do a while($row =
      mysql_query_fet ch_Assoc($Resul t)){} it will bring everything in at once and
      no opportunity to iterate on an individual level.


      "Rik" <luiheidsgoeroe @hotmail.com> wrote in message
      news:c9e25$44a1 8261$8259c69c$1 6916@news1.tude lft.nl...[color=blue]
      > Garry wrote:[color=green]
      >> How do I cycle through a MySQL query result one row at a time so that
      >> I can do some work on each individual row, instead of having the
      >> whole query scroll by. I need to have the ability to post each row
      >> to a form and then work on it, posting the results, then go on to the
      >> next row.[/color]
      >
      > Euhm, that's already how it works.
      > $result = mysql_query('so me query');
      > while($row = mysql_fetch_ass oc($result)){
      > //do some work on the $row
      > }
      >
      > Grtz,
      > --
      > Rik Wasmus
      >
      >[/color]


      Comment

      • Rik

        #4
        Re: PHP MySQL Single Iteration for data manipulation

        Garry wrote:[color=blue]
        > The problem is that when you do a while($row =
        > mysql_query_fet ch_Assoc($Resul t)){} it will bring everything in at
        > once and no opportunity to iterate on an individual level.[/color]


        First: I don't know the function 'mysql_query_fe tch_Assoc'.
        Second: No,it won't, it will give you an associative array of a single row.

        Maybe in stead of making some hasty posts, you might share what it is
        exactly what you want to accomplish. That might clarify your problem a bit.

        Grtz,
        --
        Rik Wasmus


        Comment

        • Garry

          #5
          Re: PHP MySQL Single Iteration for data manipulation

          I am trying to create a multiple choice test based on a MySQL database in
          PHP and presenting just one question at a time for the user to read and
          slect the correct answer. If the correct answer is selected a
          congratulatory message is displayed and the correct count is incremented.
          If an incorrect answer is chosen, then display the correct answer. In both
          cases increment the question count and move on to the next question.

          The main reason for storing the questions in MySQL is to create a generic
          test wrapper that can be used for any kind of Multiple choice test.


          Thank you if you have any idea on how I can process just onbe (Row/Question)
          at a time.






          "Rik" <luiheidsgoeroe @hotmail.com> wrote in message
          news:5f736$44a1 925a$8259c69c$1 8887@news1.tude lft.nl...[color=blue]
          > Garry wrote:[color=green]
          >> The problem is that when you do a while($row =
          >> mysql_query_fet ch_Assoc($Resul t)){} it will bring everything in at
          >> once and no opportunity to iterate on an individual level.[/color]
          >
          >
          > First: I don't know the function 'mysql_query_fe tch_Assoc'.
          > Second: No,it won't, it will give you an associative array of a single
          > row.
          >
          > Maybe in stead of making some hasty posts, you might share what it is
          > exactly what you want to accomplish. That might clarify your problem a
          > bit.
          >
          > Grtz,
          > --
          > Rik Wasmus
          >
          >[/color]


          Comment

          • Rik

            #6
            Re: PHP MySQL Single Iteration for data manipulation

            Garry wrote:[color=blue]
            > I am trying to create a multiple choice test based on a MySQL
            > database in PHP and presenting just one question at a time for the
            > user to read and slect the correct answer. If the correct answer is
            > selected a congratulatory message is displayed and the correct count
            > is incremented. If an incorrect answer is chosen, then display the
            > correct answer. In both cases increment the question count and move
            > on to the next question.
            >
            > The main reason for storing the questions in MySQL is to create a
            > generic test wrapper that can be used for any kind of Multiple choice
            > test.
            >
            >
            > Thank you if you have any idea on how I can process just onbe
            > (Row/Question) at a time.[/color]

            So, your problem isn't that you want to process the results individually,
            you want only 1 row from the database. Assuming you want to let them see
            only one question, and then submit a form to the next page?

            Possible solutions:
            - add a field called 'question_numbe r" or something, and query 'SELECT
            question, answer FROM table WHERE questionnumber = the variable. The
            variable could be increased after every answer, possibly using a GET or POST
            variable.

            - You could use a LIMIT $start, 1 at the end of your query, where $start is
            also incremented after every question, again a GET or POST variable seems
            logical.

            If you want to use MySQL, I would suggest reading some tutorials on SQL /
            query-building. If you want only one row, the SQL should give you only one,
            don't try to let PHP sort it out.

            Grtz,
            --
            Rik Wasmus


            Comment

            • Jerry Stuckle

              #7
              Re: PHP MySQL Single Iteration for data manipulation

              Garry wrote:[color=blue]
              > How do I cycle through a MySQL query result one row at a time so that I can
              > do some work on each individual row, instead of having the whole query
              > scroll by. I need to have the ability to post each row to a form and then
              > work on it, posting the results, then go on to the next row.
              >
              >
              > Thank You in Advance for your putting me on the right track.
              >
              >
              >[/color]

              Garry,

              You can't do it with a loop. You'll have to fetch a single row each time.

              You could do it something like:

              $last = 0;
              $result = mysql_query("SE LECT * FROM myTable WHERE id > $last ORDER BY id
              LIMIT 1");
              if ($result) { // Ensure it worked
              if (mysql_num_rows () > 0) { // And returned data
              $data = mysql_fetch_arr ay($result);
              // Process the row
              $last = $data['id'];
              }
              else {
              // End of data
              }
              }
              else {
              // Process the mysql error
              }

              Assuming id is a unique integer > 0 (or at least unique within other WHERE
              clause settings).

              --
              =============== ===
              Remove the "x" from my email address
              Jerry Stuckle
              JDS Computer Training Corp.
              jstucklex@attgl obal.net
              =============== ===

              Comment

              • Gdell

                #8
                Re: PHP MySQL Single Iteration for data manipulation

                Thank you I will try it out.



                "Rik" <luiheidsgoeroe @hotmail.com> wrote in message
                news:711f1$44a1 9ad7$8259c69c$1 9906@news1.tude lft.nl...[color=blue]
                > Garry wrote:[color=green]
                >> I am trying to create a multiple choice test based on a MySQL
                >> database in PHP and presenting just one question at a time for the
                >> user to read and slect the correct answer. If the correct answer is
                >> selected a congratulatory message is displayed and the correct count
                >> is incremented. If an incorrect answer is chosen, then display the
                >> correct answer. In both cases increment the question count and move
                >> on to the next question.
                >>
                >> The main reason for storing the questions in MySQL is to create a
                >> generic test wrapper that can be used for any kind of Multiple choice
                >> test.
                >>
                >>
                >> Thank you if you have any idea on how I can process just onbe
                >> (Row/Question) at a time.[/color]
                >
                > So, your problem isn't that you want to process the results individually,
                > you want only 1 row from the database. Assuming you want to let them see
                > only one question, and then submit a form to the next page?
                >
                > Possible solutions:
                > - add a field called 'question_numbe r" or something, and query 'SELECT
                > question, answer FROM table WHERE questionnumber = the variable. The
                > variable could be increased after every answer, possibly using a GET or
                > POST
                > variable.
                >
                > - You could use a LIMIT $start, 1 at the end of your query, where $start
                > is
                > also incremented after every question, again a GET or POST variable seems
                > logical.
                >
                > If you want to use MySQL, I would suggest reading some tutorials on SQL /
                > query-building. If you want only one row, the SQL should give you only
                > one,
                > don't try to let PHP sort it out.
                >
                > Grtz,
                > --
                > Rik Wasmus
                >
                >[/color]


                Comment

                • Gdell

                  #9
                  Re: PHP MySQL Single Iteration for data manipulation

                  PK is {QuestionNumber INT}
                  "Jerry Stuckle" <jstucklex@attg lobal.net> wrote in message
                  news:L6idnc-MdLNxATzZnZ2dnU VZ_rqdnZ2d@comc ast.com...[color=blue]
                  > Garry wrote:[color=green]
                  >> How do I cycle through a MySQL query result one row at a time so that I
                  >> can do some work on each individual row, instead of having the whole
                  >> query scroll by. I need to have the ability to post each row to a form
                  >> and then work on it, posting the results, then go on to the next row.
                  >>
                  >>
                  >> Thank You in Advance for your putting me on the right track.
                  >>
                  >>
                  >>[/color]
                  >
                  > Garry,
                  >
                  > You can't do it with a loop. You'll have to fetch a single row each time.
                  >
                  > You could do it something like:
                  >
                  > $last = 0;
                  > $result = mysql_query("SE LECT * FROM myTable WHERE id > $last ORDER BY
                  > id LIMIT 1");
                  > if ($result) { // Ensure it worked
                  > if (mysql_num_rows () > 0) { // And returned data
                  > $data = mysql_fetch_arr ay($result);
                  > // Process the row
                  > $last = $data['id'];
                  > }
                  > else {
                  > // End of data
                  > }
                  > }
                  > else {
                  > // Process the mysql error
                  > }
                  >
                  > Assuming id is a unique integer > 0 (or at least unique within other WHERE
                  > clause settings).
                  >
                  > --
                  > =============== ===
                  > Remove the "x" from my email address
                  > Jerry Stuckle
                  > JDS Computer Training Corp.
                  > jstucklex@attgl obal.net
                  > =============== ===[/color]


                  Comment

                  Working...