从多个表导出为CSV

时间:2019-05-18 07:00:48

标签: php csv pdo

我无法导出到CSV脚本。我只能一点一点地工作。它将从一个表下载,或者从另一个表下载,但不会两者都下载。基本上,我想要来自$get_asset的某些字段和来自$customfields的某些字段的标题和值查询。 array_merge函数仅在$customfields数组中将级别降低时将其从$fields数组中拉出。我什至为foreach数组尝试了$customfields,但仍然无法正常工作。非常感谢您的协助。

    /* ASSET INFO */
    $get_asset = $pdo->prepare("SELECT * FROM assets WHERE type = ?");
    $get_asset->execute([$type]);
    $get_asset = $get_asset->fetchAll(PDO::FETCH_ASSOC);

    foreach ($get_asset as $asset) {
        /* ASSET TYPES */
        $get_asset_type = $pdo->prepare("SELECT * FROM assets_type WHERE id = ?");
        $get_asset_type->execute([$asset['type']]);
        $get_asset_type = $get_asset_type->fetch(PDO::FETCH_ASSOC);

        /* CUSTOM FIELDS */
        $customfields = $pdo->prepare("SELECT * FROM assets_fields f LEFT JOIN assets_fields_values v ON f.id = v.fieldid AND v.related_asset = ? WHERE related_type = ? ORDER BY f.fieldorder ASC");
        $customfields->execute([$asset['id'], $asset['type']]);
        $customfields = $customfields->fetchAll(PDO::FETCH_ASSOC);

        $fields = array_merge($asset, $customfields);
}

0 个答案:

没有答案