我现在有一个成功运行的php脚本,它将sql查询的结果写入CSV文件,最终作为附件邮寄。到目前为止一切都那么好,但是我需要添加另一个类似的查询,基本上只进行一些求和,但是,我希望它在第一个之后运行并让它们都写入CSV。
希望/理想情况下,编写查询1的结果,跳过几个空白行,并写出查询2的结果。如果我可以为第二个查询添加一个标题,说“CSR Totals”但我是我不甘心。
无论如何,我不确定该怎么做。我会想象使用mysqli_multiple_queries?我不想因为它运作良好而重构太多我所拥有的东西,但我意识到我必须改变一些事情。这不是我的专业领域,所以我希望得到一些指导性的建议。
这是代码:(注意我已经连接了第二个查询,但只是为了显示我的概念,我不确定语法是否正确。)
$result = mysqli_query($conn2,
"SELECT
firstn
, lastn
, extension
, Recieved
, RecievedKnown
, Outbound
, outboundKnown
FROM (
SELECT
firstn
, lastn
, c.extension
,CASE WHEN LEGTYPE1 = 2 AND ANSWERED = 1 THEN 'x' ELSE '' END AS Recieved
, case when LEGTYPE1 = 2 and answered = 1 and CALLINGPARTYNO = k.phone_number then 'x' ELSE '' end as RecievedKnown
, CASE WHEN ANSWERED = 1 AND LEGTYPE1 = 1 then 'x' ELSE '' end AS Outbound
, case when LEGTYPE1 = 1 and FINALLYCALLEDPARTYNO = k.phone_number then 'x' ELSE '' end as outboundKnown
FROM test.session a
INNER JOIN test.callsummary b ON a.NOTABLECALLID = b.NOTABLECALLID
right join jackson_id.users c on a.callingpartyno = c.extension or a.finallycalledpartyno = c.extension
LEFT JOIN test.known_numbers k ON a.callingpartyno = k.phone_number
WHERE a.ts >= curdate()
and(a.CALLINGPARTYNO in (select extension from test.test_users) OR a.finallycalledpartyno IN (select extension from test.test_users))
) x
ORDER BY extension;") or die(mysqli_error( $conn2));
$result .= mysqli_query($conn2,
"//2nd query in here") or die(mysqli_error( $conn2));
if (!$result) die('Couldn\'t fetch records');
$num_fields = mysqli_num_fields($result);
$headers = array();
while ($fieldinfo = mysqli_fetch_field($result)) {
$headers[] = $fieldinfo->name;
}
$fp = fopen('test.csv', 'w');
if ($fp && $result) {
fputcsv($fp, $headers);
while ($row = $result->fetch_array(MYSQLI_NUM)) {
fputcsv($fp, array_values($row));
}
fclose($fp);
}
$file = "test.csv";