我有一个分为不同部分的网页。每个部分都必须显示不同的结果。这些结果来自数据库。 这是关于SQLfiddle的示例数据
http://sqlfiddle.com/#!9/ad98b/1
以下代码是我想到的,但我担心当不同的人多次访问此页面时,它可能会以某种方式使服务器超载
$sectionA = $connect->query("SELECT * FROM Main_Section WHERE section = `A`
");
while ($row = $sectionA->fetch_array(MYSQLI_BOTH))
{
$id = $row["id"];
$name = $row["name"];
$sec_result_a = $sec_result_a.'<p>'.$id.'</p><h3>'.$name.'</h3>';
}
$sectionB = $connect->query("SELECT * FROM Main_Section WHERE section = `B` ");
while ($row = $sectionB->fetch_array(MYSQLI_BOTH))
{
$id = $row["id"];
$name = $row["name"];
$sec_result_b = $sec_result_b.'<p>'.$id.'</p><h3>'.$name.'</h3>';
}
$sectionC = $connect->query("SELECT * FROM Main_Section WHERE section = `C` ");
while ($row = $sectionC->fetch_array(MYSQLI_BOTH))
{
$id = $row["id"];
$name = $row["name"];
$sec_result_c= $sec_result_c.'<p>'.$id.'</p><h3>'.$name.'</h3>';
}
UP to section Z
有没有办法可以正确地优化它?
答案 0 :(得分:1)
除非图片中有更多内容,为什么不查询按部分排序的所有内容,以获得A-Z:
SELECT * FROM Main_Section ORDER BY section
...然后用一个循环处理结果,看起来像这样:
$sections = $connect->query("SELECT * FROM Main_Section ORDER BY section");
while ($row = $sections->fetch_array())
{
echo $row['section'] . ' ' . '<p>' . $row['id'] . '</p><h3>' . $row['firstname'] . ' ' . $row['lastname'] . '</h3>';
}