Home > Archive > PHP Programming > June 2006 > PHP MySQL Single Iteration for data manipulation
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
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.
| |
|
| Garry wrote:
> 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.
Euhm, that's already how it works.
$result = mysql_query('some query');
while($row = mysql_fetch_assoc($result)){
//do some work on the $row
}
Grtz,
--
Rik Wasmus
| |
|
| The problem is that when you do a while($row =
mysql_query_fetch_Assoc($Result)){} 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$44a18261$8259c69c$16916@news1
.tudelft.nl...
> Garry wrote:
>
> Euhm, that's already how it works.
> $result = mysql_query('some query');
> while($row = mysql_fetch_assoc($result)){
> //do some work on the $row
> }
>
> Grtz,
> --
> Rik Wasmus
>
>
| |
|
| Garry wrote:
> The problem is that when you do a while($row =
> mysql_query_fetch_Assoc($Result)){} it will bring everything in at
> once and no opportunity to iterate on an individual level.
First: I don't know the function 'mysql_query_fetch_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
| |
|
| 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$44a1925a$8259c69c$18887@news1
.tudelft.nl...
> Garry wrote:
>
>
> First: I don't know the function 'mysql_query_fetch_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
>
>
| |
|
| Garry wrote:
> 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.
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_number" 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
| |
| Jerry Stuckle 2006-06-27, 6:58 pm |
| Garry wrote:
> 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.
>
>
>
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("SELECT * 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_array($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@attglobal.net
==================
| |
|
| Thank you I will try it out.
"Rik" <luiheidsgoeroe@hotmail.com> wrote in message
news:711f1$44a19ad7$8259c69c$19906@news1
.tudelft.nl...
> Garry wrote:
>
> 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_number" 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
>
>
| |
|
| PK is {QuestionNumber INT}
"Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
news:L6idnc-MdLNxATzZnZ2dnUVZ_rqdnZ2d@comcast.com...
> Garry wrote:
>
> 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("SELECT * 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_array($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@attglobal.net
> ==================
|
|
|
|
|