Php 如何将sql循环查询拆分为多个部分
我有一个数据库表Php 如何将sql循环查询拆分为多个部分,php,mysql,while-loop,Php,Mysql,While Loop,我有一个数据库表haccp,其中包含按8个部分分类的标准行。 我试图创建一个查询来获取所有条件并在每个部分下回显它。例如: 第1节 标准1 标准2 第2节 标准1 标准2 我尝试使用WHERE子句编写8个单独的sql查询,并使用while循环来回显每个部分下的结果,但运行和加载页面需要30秒 上一个SQL查询 $get_delivery_criteria = $mysqli->prepare("SELECT criteria, section, hazard FROM haccp WHER
haccp
,其中包含按8个部分分类的标准行。
我试图创建一个查询来获取所有条件并在每个部分下回显它。例如:
第1节
标准1
标准2
第2节
标准1
标准2
我尝试使用WHERE
子句编写8个单独的sql查询,并使用while
循环来回显每个部分下的结果,但运行和加载页面需要30秒
上一个SQL查询
$get_delivery_criteria = $mysqli->prepare("SELECT criteria, section, hazard FROM haccp WHERE section='Delivery' ORDER BY criteria");
$get_delivery_criteria->execute();
$get_delivery_criteria->bind_result($criteria_db, $section, $hazard);
$get_delivery_criteria->store_result();
$row_cnt = $get_delivery_criteria->num_rows();
if($row_cnt >= 1)
{
while ($get_delivery_criteria->fetch() )
{
?>
<li><a href="?criteria=<? echo $criteria_db; ?>"><? echo $criteria_db. " ". $hazard; ?></a></li>
<?
}
$get_delivery_criteria->close();
}
$sections = array("1.Delivery", "2.Storage", "3.Preparation", "4.Cooking", "5.Serving", "6.Cleaning", "7.Building", "8.Management");
$get_criteria = $mysqli->prepare("SELECT criteria, section, hazard FROM haccp ORDER BY criteria");
$get_criteria->execute();
$get_criteria->bind_result($criteria_db, $section_db, $hazard_db);
$get_criteria->store_result();
$row_cnt = $get_criteria->num_rows();
if($row_cnt >= 1)
{
while ($get_criteria->fetch() )
{
foreach ($sections as $section)
?>
<p class="haccp-section"><strong><? echo $section; ?></strong></p>
<div class="divider"></div>
<li><a href="?criteria=<? echo $criteria_db; ?>"><? echo $criteria_db. " ". $hazard_db; ?></a></li>
<?
}
}
$get_criteria->close();
然而,这是呼应每个标准的章节标题,而不是下面标准列表的标题
希望这是有意义的,并提前感谢 我理解你的问题。您可能希望根据节对数据进行分组。为此,请尝试以下代码:
<?php
$get_criteria = $mysqli->prepare("SELECT criteria, section, hazard FROM haccp ORDER BY section,criteria");
$get_delivery_criteria->execute();
$get_delivery_criteria->bind_result($criteria_db, $section, $hazard);
$get_delivery_criteria->store_result();
$row_cnt = $get_delivery_criteria->num_rows();
if($row_cnt >= 1)
{
$current_section="";
while ($get_delivery_criteria->fetch() )
{
if($section != $current_section)
{ ?>
<p class="haccp-section"><strong><? echo $section; ?></strong></p>
<div class="divider"></div>
<?php
$current_section=$section;
} ?>
<li><a href="?criteria=<? echo $criteria_db; ?>"><? echo $criteria_db. " ". $hazard; ?></a></li>
<?
}
$get_delivery_criteria->close();
}
@B.德赛
我已经调整了您的查询以使用不正确的$section\u db
。这项工作现在如预期的那样起作用:
$get_criteria = $mysqli->prepare("SELECT criteria, section, hazard FROM haccp ORDER BY criteria");
$get_criteria->execute();
$get_criteria->bind_result($criteria_db, $section_db, $hazard_db);
$get_criteria->store_result();
$row_cnt = $get_criteria->num_rows();
if($row_cnt >= 1)
{
$current_section="";
while ($get_criteria->fetch() )
{
if($section_db != $current_section)
{
$current_section=$section_db;
?>
<p class="haccp-section"><strong><? echo $section_db; ?></strong></p>
<div class="divider"></div>
<?
}
?>
<li><a href="?criteria=<? echo $criteria_db; ?>"><? echo $criteria_db. " ". $hazard_db; ?></a></li>
<?
}
$get_criteria->close();
}
$get_criteria=$mysqli->prepare(“根据标准从haccp订单中选择标准、章节、危害”);
$get_criteria->execute();
$get\u criteria->bind\u result($criteria\u db、$section\u db、$hazard\u db);
$get_criteria->store_result();
$row_cnt=$get_criteria->num_rows();
如果($row\u cnt>=1)
{
$current_section=“”;
而($get\u criteria->fetch())
{
如果($section\u db!=$current\u section)
{
$current_section=$section_db;
?>
这意味着我必须将当前节指定为一个变量,因此对每个节执行此操作,这意味着8个变量和8个查询。这就是我的第一个查询-我只是使用了一个WHERE
子句来指定每个节。我真的在寻找一个查询。@user3464091看到我的整个解决方案了吗?我不是calling quey 8次。仅一次。请查看正确答案YIT不起作用-在
中返回空值的函数fetch()。您没有从数组或数据库中设置$section
?函数fetch()在
中为空不是逻辑问题。请检查您的查询是否正确执行。我只添加了额外的
ORDER BY
字段,该字段是第一次尝试查询中的
部分
。如果您的第一次查询成功执行,则必须确保您只更改了变量,即$section
到$section\u db
。您是declaring数组但我看不出你在你发布的答案代码中的任何地方都使用了该数组。我不明白部分如何从if
子句中只回显一次,并且没有在循环中重复?是的,但我将这行$current_section=$section_db;
移到
之前作为变量wasn未通过。这是因为条件。只有当部分
更改时,它才会为真。我明白了。因为$current\u部分
设置在循环之外,而循环?