【问题标题】:How to retrieve related child rows as I loop through rows from the parent table?当我遍历父表中的行时,如何检索相关的子行?
【发布时间】:2015-02-13 15:08:10
【问题描述】:

当我遍历外部查询的行时,我需要运行子查询(或其他东西)。我已尽我所能尝试,但无法使其正常工作。

这是我的 while 循环:

$sql = "SELECT * FROM $db ORDER BY Created DESC";
$ps = $pdo->prepare($sql);
if (!$ps) {
    echo "\nPDO::errorInfo():\n";
    print_r($dbh->errorInfo());
}else{
    $ps->execute();
    $ps->setFetchMode(PDO::FETCH_OBJ);
    while($row = $ps->fetch()) {

        echo "<tr>\n";

            echo "  <td>". $row->SARNo . "</td>\n";
            echo "  <td>". $row->Quals . "</td>\n";
            echo "  <td>". <!-- how do I insert results of $query here? --> . "</td>\n";
            echo "  <td>". $row->CAGE . "</td>\n";
            echo "  <td>". $row->Supplier_Name . "</td>\n";
            echo "  <td>". $row->Assigned_To_HEBCO . "</td>\n";
            echo "  <td>". $row->SAR_Completed . "</td>\n";

        echo "</tr>\n";

    }
}

这是获取当前记录的关联 NSN 的查询:

$query = "SELECT NSN_Src.NSN FROM NSN_Src INNER JOIN (SAR2 INNER JOIN REL_SAR_NSN ON SAR2.ID = REL_SAR_NSN.SAR_ID) ON NSN_Src.ID = REL_SAR_NSN.NSN_ID WHERE (((SAR2.ID)=$uid))";

我对 Access 不是很熟悉,更不熟悉将 Access 与 PHP 一起使用,所以这个小项目由于缺少 PHP 函数(与可用于MySQL)。

非常感谢任何帮助,并且一个可行的解决方案将使您按照我的意愿购买我 50% 的世俗物品(这可能是一大笔债务,所以如果你愿意,你可以拒绝)。 :)

【问题讨论】:

  • 只是一个猜测,但您是否正在寻找一种方法来获取多个 NSN 值的列表,作为 MySQL 的 GROUP_CONCAT() 会产生的单个字符串?
  • 是的,和 GROUP_CONCAT() 完全一样

标签: php sql ms-access pdo


【解决方案1】:

您可能已经发现,Access SQL 没有像 MySQL 的 GROUP_CONCAT() 这样的聚合函数。因此,您需要让您的 PHP 代码使用第二个准备好的语句和一个内部循环来创建子项列表,如下所示:

<?php
header('Content-Type: text/html; charset=windows-1252');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title>PDO example</title>
</head>
<body>
<?php
$connStr = 
        'odbc:' .
        'Driver={Microsoft Access Driver (*.mdb)};' .
        'Dbq=C:\\Users\\Public\\__SO\\28502544.mdb;' .
        'Uid=Admin;';
$db = new PDO($connStr);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sqlParent = "SELECT ID, ParentName FROM Parent";
$psParent = $db->prepare($sqlParent);

$sqlChild = "SELECT ChildName FROM Child WHERE ParentID = ?";
$psChild = $db->prepare($sqlChild);
$psChild->bindParam(1, $parentID, PDO::PARAM_INT);

echo '<table border=1>';
$psParent->execute();
while ($rowParent = $psParent->fetch()) {
    echo '<tr>';
    echo '<td>';
    echo $rowParent["ParentName"];
    echo '</td>';
    // collect child items into array
    $parentID = $rowParent["ID"];
    $psChild->execute();
    $childItems = array();
    while ($rowChild = $psChild->fetch()) {
        $childItems[] = $rowChild["ChildName"];
    }
    // string together and insert into table cell
    echo '<td>';
    echo implode(", ", $childItems);
    echo '</td>';
    echo '</tr>';
}
echo '</table>';
?>
</body>
</html>

【讨论】:

  • 优秀。谢谢你的帮助。使用 PHP 和 Access 很奇怪。欣赏指导和示例。
猜你喜欢
  • 2014-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-23
  • 1970-01-01
相关资源
最近更新 更多