PHP比较两个while循环的结果
我有以下疑问:PHP比较两个while循环的结果,php,mysql,Php,Mysql,我有以下疑问: $state = get['state']; $getPossibleIDs = "SELECT * FROM ".$report_group_lookup; $qPossibleID = $conn->prepare($getPossibleIDs); $qPossibleID -> execute(); while($rowPossible = $qPossibleID->fetch())
$state = get['state'];
$getPossibleIDs = "SELECT * FROM ".$report_group_lookup;
$qPossibleID = $conn->prepare($getPossibleIDs);
$qPossibleID -> execute();
while($rowPossible = $qPossibleID->fetch())
{
$possibleID = $rowPossible['id'];
$possibleName = $rowPossible['name'];
$getSpecificIDs = "SELECT * FROM rbs_report_type_2_specific WHERE rbs_report_type_id =".$state;
$qSpecifcID = $conn -> prepare($getSpecificIDs);
$qSpecifcID -> execute();
while($rowSpecfics = $qSpecifcID->fetch())
{
$specificIDs = $rowSpecfics['rbs_specific_id'];
if($possibleID == $specificIDs)
{
echo $possibleName."-Yes<br/>";
}
else
{
echo $possibleName."-No<br/>";
}
}
}
因此,我得到的结果重复了三次,每个rbs_特殊id重复一次:
HDD Coordinator-No
HDD Coordinator-No
HDD Coordinator-No
Rig Manager-Yes
Rig Manager-No
Rig Manager-No
Driller-No
Driller-Yes
Driller-No
Tank Hand-No
Tank Hand-No
Tank Hand-Yes
而不是:
HDD Coordinator-No
Rig Manager-Yes
Driller-Yes
Tank Hand-Yes
关于如何相应地更改输出有什么建议吗
谢谢如果您只查找第一个值,那么您可以完全避免使用第二个while循环。你可以这样做:
$getPossibleIDs = "SELECT * FROM ".$report_group_lookup;
$qPossibleID = $conn->prepare($getPossibleIDs);
$qPossibleID -> execute();
while($rowPossible = $qPossibleID->fetch()) {
$possibleID = $rowPossible['id'];
$possibleName = $rowPossible['name'];
$getSpecificIDs = "SELECT * FROM rbs_report_type_2_specific WHERE rbs_report_type_id =".$state;
$qSpecificID = $conn -> prepare($getSpecificIDs);
$qSpecificID -> execute();
$rowSpecifics = $qSpecificID->fetch()
if ($rowSpecifics) { //if a value is present then continue execution
$specificIDs = $rowSpecifics['rbs_specific_id'];
if ($possibleID == $specificIDs) {
echo $possibleName."-match<br/>";
} else {
echo $possibleName."-No<br/>";
}
}
}
$getPossibleIDs=“从“$report\u group\u lookup”中选择*;
$qPossibleID=$conn->prepare($GetPossibleId);
$qPossibleID->execute();
而($rowPossible=$qPossibleID->fetch()){
$possibleID=$rowPossible['id'];
$possibleName=$rowPossible['name'];
$GetSpecificId=“从rbs_报告类型_2_-specific中选择*,其中rbs_报告类型_-id=“.$state;
$qSpecificID=$conn->prepare($getSpecificIDs);
$qSpecificID->execute();
$rowspecics=$qSpecificID->fetch()
如果($rowSpecifics){//如果存在值,则继续执行
$specificIDs=$rowspecits['rbs_specific_id'];
如果($possibleID==$specificId){
echo$possibleName。“-match
”;
}否则{
echo$possibleName。“-否
”;
}
}
}
这将检查第二次查找中是否存在值。如果是这样的话,它将执行您的逻辑,但只执行一次。不要执行内部循环,而是使用连接查询在第一个查询中执行所有操作
$getPossibleIDs = "
SELECT rgl.id, rgl.name,
CASE WHEN rrt2s.rbs_specific_id IS NOT NULL THEN 'Yes' ELSE 'NO' END as `match`
FROM ".$report_group_lookup." rgl
LEFT JOIN rbs_report_type_2_specific rrt2s
ON rrt2s.rbs_specific_id = rgl.id
AND rrt2s.rbs_report_type_id = ?";
$qPossibleID = $conn->prepare($getPossibleIDs);
$qPossibleID -> execute(array($state));
while($rowPossible = $qPossibleID->fetch())
{
echo $rowPossible['name']. " -".$rowPossible['match']."<br />";
}
$getPossibleIDs=”
选择rgl.id、rgl.name、,
如果rrt2s.rbs_特定_id不为空,则“是”或“否”以“匹配”结尾`
来自“$report\u group\u lookup.”rgl
左连接rbs\u报告\u类型\u 2\u特定rrt2s
在rrt2s.rbs_-specific_-id=rgl.id上
和rrt2s.rbs_report_type_id=?”;
$qPossibleID=$conn->prepare($GetPossibleId);
$qPossibleID->execute(数组($state));
而($rowPossible=$qPossibleID->fetch())
{
echo$rowPossible['name'].“-”$rowPossible['match'.“
”;
}
已编辑,但状态只是一个问题,您可能会受到SQL注入攻击。使用参数化查询可以完全避免此问题。您确实应该了解SQL连接。所有的循环/查询都可以在数据库中使用一个JOIN
ed查询来完成。对不起,巴德,这并不是我想要的,对于司钻和坦克手来说仍然是失败的。
$getPossibleIDs = "
SELECT rgl.id, rgl.name,
CASE WHEN rrt2s.rbs_specific_id IS NOT NULL THEN 'Yes' ELSE 'NO' END as `match`
FROM ".$report_group_lookup." rgl
LEFT JOIN rbs_report_type_2_specific rrt2s
ON rrt2s.rbs_specific_id = rgl.id
AND rrt2s.rbs_report_type_id = ?";
$qPossibleID = $conn->prepare($getPossibleIDs);
$qPossibleID -> execute(array($state));
while($rowPossible = $qPossibleID->fetch())
{
echo $rowPossible['name']. " -".$rowPossible['match']."<br />";
}