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. 8 2322
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" <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
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" <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
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
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
==================
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
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 ================== This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: ensnare |
last post by:
I'm attempting to create a threaded comment system involving PHP /
MySQL. Currently, I have a table called comments which looks like:
Table Comments: (comment_id, comment_root_id,...
|
by: Stat |
last post by:
Greetings all. I am writing a profile creator script where a user gets
a URL invite in their mail in the form of;
http://domain.com/profile-create.php?access_code=Ikd98jadf098asdf
Things are...
|
by: Philip Stoev |
last post by:
Hi all,
Please tell me if any of this makes sense. Any pointers to relevant
projects/articles will be much appreciated.
Philip Stoev
http://www.stoev.org/pivot/manifest.htm
...
|
by: w_curtis |
last post by:
I'm an Access user, and I'm trying to learn MySQL and then PHP so I can make
some web databases. But it just isn't clicking.
I've followed some tutorials, and picked up a book, but just getting...
|
by: Cheryl Langdon |
last post by:
Hello everyone,
This is my first attempt at getting help in this manner. Please
forgive me if this is an inappropriate request.
I suddenly find myself in urgent need of instruction on how to...
|
by: Jacques Lebastard |
last post by:
I have a problem writing PHP ODBC scripts that suit both MySQL and
PostgreSQL. So far, the following syntaxes seem to apply to each
database (this is an 'insert' example; the same differences apply...
|
by: Coldfire |
last post by:
Since i cannot show the differences in a two-column like table. I am first putting
MS SQL Server 2005 and then MySQL 5.x.
MS SQL Server 2005
Brief Overview
- SQL Server is a full-fledged...
|
by: Atli |
last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users.
Anyone should be able to get...
|
by: sirfsaif |
last post by:
I have three servers and each server has separate MySQL and DB.
For instance I have common DB i.e. store on all the three servers i.e web1, web2 and web3.
I m running a query in DB store on...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |