kpuc313
kpuc313

Reputation: 15

PHP - mysqli_query in foreach loop

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

Answers (1)

Barmar
Barmar

Reputation: 780714

Yes, this is OK. mysqli_query returns a mysqli_result object. The documentation says:

5.4.0 Iterator support was added, as mysqli_result now implements Traversable.

Upvotes: 3

Related Questions