以下哪个选项更好
选项1:
//control file:
$smpt = $con->query("SELECT id,name FROM customers");
//fetch all resuts
$results = array();
while ($row = $result->fetch_assoc()) {
$results[] = $row;
}
$smpt->close();
//Note: PHP version < 5.3.0 so I can't use just $results->fetch_all('MYSQLI_ASSOC');
//view file:
foreach($results as $key=>$value) {
//display results
}
unset($results);
选项 2:
//control file:
$smpt = $con->query("SELECT id,name FROM customers");
//fetch all resluts
//view file:
while($row = $result->fetch_assoc()) {
//display results
}
$smpt->close();
我正在尝试将逻辑与演示完全分开......当前我正在使用选项 2,因为使用选项 1,脚本会经历 2 个循环。一个用于获取数据,另一个用于显示它们。但是哪个更好用?
谢谢