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

PHP MySQL Single Iteration for data manipulation

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

Jun 27 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Rik
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
Jun 27 '06 #2

P: n/a
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" <lu************@hotmail.com> wrote in message
news:c9***************************@news1.tudelft.n l...
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

Jun 27 '06 #3

P: n/a
Rik
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
Jun 27 '06 #4

P: n/a
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" <lu************@hotmail.com> wrote in message
news:5f***************************@news1.tudelft.n l...
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

Jun 27 '06 #5

P: n/a
Rik
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
Jun 27 '06 #6

P: n/a
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.
js*******@attglobal.net
==================
Jun 27 '06 #7

P: n/a
Thank you I will try it out.

"Rik" <lu************@hotmail.com> wrote in message
news:71***************************@news1.tudelft.n l...
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

Jun 28 '06 #8

P: n/a
PK is {QuestionNumber INT}
"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:L6******************************@comcast.com. ..
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.
js*******@attglobal.net
==================

Jun 28 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.