There are different ways, how to fetch and print MySQL data with PHP.
For example, you can fetch data row by row with PHP loop:
$result = $mysqli->query("SELECT * FROM `table`");
while($data = $result->fetch_assoc())
{
echo '<div>'. $data["field"] .'</div>';
}
Also, you can store all selected data into array, and then go through it:
$result = $mysqli->query("SELECT * FROM `table`");
$data = $result->fetch_all(MYSQLI_ASSOC);
foreach($data as $i => $array)
{
echo '<div>'. $array["field"] .'</div>';
}
Is there any serious reason, why I should use one method instead of the other? And what about performance in case of enermous databases?
In the first example
while($data = $result->fetch_assoc())you call thefetch_assocfunction for each time you perform a loop. Second one just calls thefetch_allonce, stores the data in an array and later just use it. So, in theory the second approach should be faster, however you’d better to do a simple benchmark to make sure.