Forum Moderators: coopster
mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() withMYSQL_ASSOCfor the optional second parameter. It only returns an associative array. This is the way mysql_fetch_array() originally worked. If you need the numeric indices as well as the associative, use mysql_fetch_array().Resource:
mysql_fetch_assoc() [php.net]
As far as which control structure to use -- that will depend on how you want to control the flow of the logic in your script. The first iteration of a do-while [php.net] loop is going to run no matter what whereas a while [php.net] loop will run as long as the expression is true. You can always break [php.net] from either.
A switch doesn't loop through the result set, it's more like a bunch of
ifstatements. If you only had one row being returned and wanted to check it against some values, the switch would be a control structure you might use, but not for looping through a result set.
The first iteration of a do-while loop is going to run no matter what whereas a while loop will run as long as the expression is true.
So the evaluation of whether the "while" condition is true falls after the first iteration of the "do...while" loop, whereas the evaluation of "while" happens before the first iteration of a "while" loop.
Gotcha. Thanks, again.
And performance characteristics are similar, depending on what is happening inside the loops? Or are they virtually interchangeable, more a matter of coding style than a practical matter?
mysql_fetch_array() and mysql_fetch_assoc()
If I need the indices, use fetch_array. If I don't, use fetch_assoc, which will use fewer resources. Yes?
You can make fetch_array do the same as either fetch_assoc or fetch_row using the second parameter.
In other words there's no need for either function unless you think it makes your code more clear.