This is kind of dated information. In one way you're asking a question that is just accepted along the community. It is understood better that while I have information to show ... do something. You generally wouldn't say foreach of these items ... do something though you could. However, the other problem is mysql_fetch_array returns FALSE if there are no more rows
. This would not work in a foreach because it is not an array. A for would also fail because to check for the finishing of a for you have to go to some point and end.. FALSE is not a valid point of check (that I have ever tried or used).
@Michael Berkowski Adds:
As of PHP 5.4+, the mysqli_result class does have Iterator support, meaning you can do $result = mysqli_query(...);
and subsequently foreach ($result as $row) {...}
and it will fetch associative arrays
Though this isn't to be said the most commonly used form which is why we have the question.
You could do...while
but why would you. You don't have most the information that you're going to need from the fetch array.
While is accepted and generally better. Doesn't fail and has a fall back if the mysql fails any way.
Lastly... don't use mysql_* anymore. Switch to mysqli_*. Safer.. smarter.. better.