[英]making a foreach loop inside an SQL query or do it dynamically via PHP?
我的DB中有2张桌子,息肉和插图。 在插图中,息肉的PK为FK。 我想做的是:
SELECT polyptychID FROM Polyptychs
随后,foreach ID返回了,我需要所有插图。 通过PHP,解决方案是这样的(使用PDO sintax):
<?php
//create the connection with DB
$sql = "SELECT polyptychID, polyptych_image FROM Polyptychs";
$stmt = $this->DBH->query($sql);
$resultTmp = $stmt->fetchAll(PDO::FETCH_ASSOC);
$final_result = array();
foreach($resultTmp as $val){
$id = $val['polyptychID'];
$final_result["$id"]["image"] = $val['polyptych_image'];
$sql2 = "SELECT * FROM Illustrations WHERE polyptychID = :polyID";
$stmt2 = $this->DBH->prepare($sql2);
$stmt2->execute(array('polyID' => $id));
$resultTmp2 = $stmt2->fetchAll(PDO::FETCH_ASSOC);
$final_result["$id"]["illustrations"] = $resultTmp2;
unset($id);
unset($sql2);
unset($stmt2);
unset($resultTmp2);
}
?>
现在$final_result
包含所有polyptychID
作为数组的键及其相对的图像和插图(如果代码中没有错误)。 我想知道的是,是否有更简单的方法来获取它(也许通过SQL进行),什么是最佳解决方案。 谢谢
您可以使用内部联接只运行一个查询:
SELECT P.polyptychID, polyptych_image, I.*
FROM Polyptychs P
INNER JOIN Illustrations I ON I.polyptychID = p.polyptychID
现在,您将结果循环,将它们添加到与键和illustrations
相同的数组结构polyptychID
中:
<?php
$sql = "SELECT P.polyptychID, polyptych_image, I.*
FROM Polyptychs P
INNER JOIN Illustrations I ON I.polyptychID = p.polyptychID";
$stmt = $this->DBH->query($sql);
$resultTmp = $stmt->fetchAll(PDO::FETCH_ASSOC);
$final_result = array();
foreach($resultTmp as $val){
$id = $val['polyptychID'];
$final_result["$id"]["image"] = $val['polyptych_image'];
// I'm not sure what's the ID for Illustration table, so I'll assume `illustrationID`
$final_result["$id"]["illustrations"][$val["illustrationID"]] = $val;
unset($id);
}
?>
如果我是你,这就是我要做的:
SELECT p.polyptychID as ID,
p.polyptych_image as image,
i.*
FROM Polyptychs p
INNER JOIN Illustrations i
ON i.polyptychID = p.polyptychID
根据您的评论,检查此便捷的图表以了解为什么我在这里选择使用INNER JOIN
:
然后,以所需的方式格式化数组,只需编写以下代码:
$formatted = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$id = $row['ID'];
if (!isset($formatted[$id]))
{
$formatted[$id] = array(
'image' => $row['image'],
'illustrations' => array() //we'll set this in a minute
);
}
unset($row['ID'], $row['image']);//remove id & image values
$formatted[$id]['illustrations'][$row['illustrationsID']] = $row;//assign rest of result-set
}
当然,如果Illustrations
中的字段称为ID
或image
,则必须将查询更改为p.polyptychID AS p_ID
或其他名称。 只要确保别名是唯一的就可以了。
在重新使用准备好的语句时,这似乎是人们容易忽略的地方,但是您确实可以反复使用相同的准备好的语句。 实际上,很幸运的是,我今天早上才写了一个关于这件事的答案 。 我也使用一些可能适用于您的案例的基本示例。 因此,如果由于某种原因而无法进行联接,则可以简单地编写:
$stmt = $pdo->prepare(
'SELECT * FROM Illustrations WHERE polyptychID = :pId'
);
$baseStmt = $pdo->query(
'SELECT polyptychID, polyptych_image FROM Polyptychs'
);
$result = array()
while($row = $baseStmt->fetch(PDO::FETCH_ASSOC))
{
$result[$row['polyptychID']] = array(
'image' => $row['polyptych_image'],
'illustrations' => null
);
$stmt->execute( array(':pId' => $row['pId']));
$result[$row['polyptychID']]['illustrations'] = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.