How JustAnswer Works:
  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.
Ask Matthew Gill Your Own Question
Matthew Gill
Matthew Gill, Programming Tutor
Category: Programming
Satisfied Customers: 25
Experience:  I have over 5 years experience as a Teaching Assistant for 15 programming courses at a 4-year university.
74898053
Type Your Programming Question Here...
Matthew Gill is online now
A new question is answered every 9 seconds

I am having problems with my while loop it is only running

This answer was rated:

I am having problems with my while loop it is only running the mysql function code once. Since num equals 3 should go through 3 times. please see attached file.


 


 


http://wikisend.com/download/579462/loopjustanswer.docx


Martin :

Hello, welcome to JustAnswer my name is XXXXX XXXXX I'm one of the experts who will be assisting you today.

There doesn't seem to be any PHP experts online at the moment but I have experience with other languages and would like to suggest something if that's ok with you?

Martin :

What would happen if you tried storing num with double quotes " " instead of single quotes or no quotes at all? Does that change anything for you?

Customer:

I think i stated the question wrong. It is going thru 3 times but the mysql_query($query1) or die ('Error saving Daily record'); function is only running one time

Customer:

I put the echo in and I see it printing to to screen 3 times.

Martin :

Oh ok so the loop works fine

Martin :

So it mustn't be evaluating the mysql query

Martin :

I will opt out and see if we can find you someone that knows mysql

Customer:

ok thanks

Do you just need to go through the loop three times? If so, why not use a MySQL
LOOP
statement/function, rather than a
WHILE
? see http://dev.mysql.com/doc/refman/5.5/en/loop.html

so that your code block would become something like this:

http://wikisend.com/download/474896/MySQL LOOP instead of WHILE.txt


If you'd rather have the WHILE statement patched up, or explained at least as to why it doesn't work, just let me know. But I think that what I offer is a practical answer to the problem.


- If there is anything lacking in this answer, please " Reply " letting me know, and I will try to improve upon my answer.
- If this is the Answer that helps with the issue, then rating my Answer positively would be appreciated.
Customer: replied 3 years ago.

the value of num will vary. Can I replace the number 3 in your answer with the variable num?

Yes, like

http://wikisend.com/download/362986/MySQL LOOP instead of WHILE with num.txt

num becomes the delimiter of how many times the loop iterates.

If it were some other programming code, of course, there might be a question of scope involved. Not in MySQL I don't think :)



- If there is anything lacking in this answer, please " Reply " letting me know, and I will try to improve upon my answer.
- If this is the Answer that helps with the issue, then rating my Answer positively would be appreciated.
Customer: replied 3 years ago.


i tried it but i got errors. I am using php and that procedure is not working

PHP and MySQL are quite different.

I had the impression that you were asking about a loop in MySQL coding, not in PHP; that accounts for the problems, I think.

Since I am not a PHP expert, I must Opt Out. Another Expert will take up the case by and by. Thanks.
Hello. My name is XXXXX XXXXX I'd like to help you with this problem. Could you please copy and paste your code into the reply window? I can better diagnose the problem this way. Thank you.
Disregard my previous request. I have downloaded the the document. I will get back with you momentarily
Place

$query1="UPDATE $TableName SET Attendance_Code='".$AC[$i]."' , Absence_Date='".$mydate."' WHERE Last_Name='".$LN[$i]."' AND First_Name='".$FN[$i]."'";

in the loop before the mysql_query() statement, and run the code. Please let me know if that works.
Matthew Gill, Programming Tutor
Category: Programming
Satisfied Customers: 25
Experience: I have over 5 years experience as a Teaching Assistant for 15 programming courses at a 4-year university.
Matthew Gill and 2 other Programming Specialists are ready to help you
Customer: replied 3 years ago.


That worked. thanks for your help.

No problem. Do you understand why it wasn't working before?
Customer: replied 3 years ago.


I think it didn't work before because I defined the query outside the loop and when it went looped a second time the query1 was not declared so it just skipped it but I am not sure if this is the correct answer.

Your are partly correct. You did define the query outside the loop, as well as i and j. The first iteration of the loop (where i and j were 0) was correct, but because the query was not being updated in the loop, the values weren't changing. Essentially, the 'query1' value was set outside the loop, and that set value was used instead of the derived value you wanted. In other languages this wouldn't be such a big deal because you could use pointers. Good job!
Customer: replied 3 years ago.

THANKS FOR THE FOLLOW UP INFO. I REALLY APPRECIATE IT.

No problem! That's what it's about!!! Enjoy your day!

Related Programming Questions