Reputation: 15
First sorry if you think the question is stupid but im new in php.., so the question is: Is it okay to use foreach loop instead of while loop?
Here is an example of what I have in mind:
foreach(mysqli_query($db_connect, 'SELECT * FROM exampletable') as $row)
{
echo $row['exampleitem'];
}
It's working, but I'm not sure is it right, secure, slow and etc..
Upvotes: 0
Views: 712
Reputation: 780714
Yes, this is OK. mysqli_query
returns a mysqli_result
object. The documentation says:
5.4.0
Iterator
support was added, asmysqli_result
now implementsTraversable
.
Upvotes: 3