php - Mysqli Retrieve data and display them - best way -
which 1 of options below better
option 1:
//control file: $smpt = $con->query("select id,name customers"); //fetch resuts $results = array(); while ($row = $result->fetch_assoc()) { $results[] = $row; } $smpt->close(); //note: php version < 5.3.0 can't use $results->fetch_all('mysqli_assoc');
//view file: foreach($results $key=>$value) { //display results } unset($results);
option 2:
//control file: $smpt = $con->query("select id,name customers"); //fetch resluts
//view file: while($row = $result->fetch_assoc()) { //display results } $smpt->close();
i trying separate logic presentation... current using option 2 because option 1 script go through 2 loops. 1 fetch data , other display them. 1 better use?
thanks
option 1 allows re-use $data
variable can display results twice, cost of potentially have large amount of data stored in variable. can clear using unset($data)
once 100% sure you've finished it.
option 2 requires less loops (only 1 instead of two) , doesn't need variable store data. cost of while
loop can used once.
in grand scheme of things, differences in speed minimal user won't notice them, if there 20+ instances of in once script have noticeable affect.
i recommend option 2 providing you'd need use while
loop once. also, benchmark scripts see how perform against 1 another.
Comments
Post a Comment