They are two functions of mysqli_result.
In this way, query for each row to the database?
And in this way, only query once to the database?
I do not know how to do it in the best way and the most efficient way.
Put examples:
1.-fetch_array():
$resultUsers = getAllUsers($db);
while($row = $resultUsers->fetch_array()){
echo $row['name'];
}
2.- fetch_all():
$resultUsers = getAllUsers($db);
foreach ($resultUsers->fetch_all(MYSQLI_ASSOC) as $value) {
echo $value['name'] . "<br>";
}
2.A.-
foreach (getAllUser($db)->fetch_all(MYSQLI_ASSOC) as $value) {
echo $value['name'] . "<br>";
}
If there is a better way to do it, I hope your help.
For your particular case you need fetch_all() moved inside, as a function called getAllUsers() should get you all users, not mysqli result.
So it should be
$allUsers = getAllUsers($db);
and then $allUsers sent to a template for the output like
<?php foreach ($allUsers as $user) { ?>
<?=$user['name']?>
>?} ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With