I have known PHP for a while, but have not known how the foreach() command works. I do know that it is similar to while(). I have looked around at how it works, but am not quite sure about how you would connect mySQL statements to it.
Say for example you had an SQL statement as follows:
"SELECT username,password,email,dob FROM users"
How would you implement this into a foreach() statement to echo every user’s username, password, email and date of birth?
Thanks for all help in advance!
The
foreachmethod loops over all the rows of an array (or object), from the first until the last. This differs from awhileloop in that way that awhilekeeps on looping until a certain condition is met. This can be after 2 iterations or 1000 iterations, depending on which condition you set.In a
foreachloop, you know that there will be as many iterations as there are keys in the array (unless you use abreakstatement within it, that aborts it right away).SQL results can also be returned as array or object, which can be looped over. For example: