简体   繁体   English

PHP - Oracle查询CSV创建,在同一行返回结果

[英]PHP - Oracle query CSV creation, return results on same row

First of all, I apologise if the question I ask here is stupid, I have tried to find the answer online but I can't seem to put the search term in a short amount of words (that manages to answer my question). 首先,如果我在这里提出的问题是愚蠢的,我很抱歉,我试图在网上找到答案,但我似乎无法将搜索词放在一个简短的单词中(设法回答我的问题)。 Secondly, I know that the answer to my question is very simple, but I am fairly new to PHP/Scripting and have been stumped for hours on this trying to find a solution. 其次,我知道我的问题的答案非常简单,但我对PHP / Scripting相当新,并且在尝试寻找解决方案时花了好几个小时。

I have a PHP script which queries an Oracle database, outputs to a CSV file then emails the file to me. 我有一个PHP脚本查询Oracle数据库,输出到CSV文件然后通过电子邮件发送给我。 This is great except I want it all the results for the query to output to a single row. 这很好,除了我想要查询输出到单行的所有结果。

I want to set up an automation which runs multiple queries (all with around 5-10 rows each) to output to a CSV file. 我想设置一个运行多个查询的自动化(每个查询大约5-10行),以输出到CSV文件。 An extract of the php file is as below: php文件的摘录如下:

$csvLength = fputcsv($tmp, array(
    'Code',
    'count(*)'
));

while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
{
    $csvLength += fputcsv($tmp, array(
        $row['Code'],
        $row['COUNT(*)']
    ));
}

rewind($tmp);
$csvContent = fread($tmp, $csvLength);
fclose($tmp);

This will write a CSV file with the output something like this: 这将写一个CSV文件,其输出如下:

Code,count(*)
APPLE,201
ORANGE,504
BANANA,864

What I want is for the output to go something like this: 我想要的输出是这样的:

Code,count(*),Code,count(*),Code,count(*)
APPLE,201,ORANGE,504,BANANA,864

(The top row does not necessarily need to output more than one rep) (顶行不一定需要输出多个代表)

I can then add on to the php script to include more queries so the created csv file will end up like this: 然后我可以添加到php脚本以包含更多查询,以便创建的csv文件最终会像这样:

Run blah.php from dates 21/11/2011 to 27/11/2011...
Code,count(*),Code,count(*),Code,count(*)
APPLE,201,ORANGE,504,BANANA,864

Reason,count(*),Reason,count(*),Reason,count(*)
Rotten,10,Bruised,42,Fermented,67

Region,count(*),Region,count(*),Region,count(*)
Oddtown,503,Newtown,204,Averagetown,631

Thanks in advance for anyone that can answer my question. 提前感谢能够回答我问题的任何人。 Again I can imagine the answer is probably very simple, but anything I try seems to give me errors and I can't seem to find the answer online anywhere. 我可以想象答案可能很简单,但我尝试的任何东西似乎都给我错误,我似乎无法在任何地方找到答案。

I'd put my results in an array like so: 我将结果放在一个数组中:

$arr = array('Apple', '201', 'Orange', 504);

Then I'd use fprintf and implode to print them in a row: 然后我会使用fprintfimplode连续打印它们:

if (!($fp = fopen('file.txt', 'w'))) {
   exit;
}
fprintf($fp, "%s\n",implode(",", $arr));

This will output 这将输出

Apple,201,Orange,504 苹果,201,橙色,504

For the column names all you have to do is get the number of results and print them that many times. 对于列名,您所要做的就是获取结果数并多次打印它们。 For example if there were 5 results I'd do this: 例如,如果有5个结果我会这样做:

fprintf($fp, "%s\\n", implode( ",", array_fill( 0, 5, "Code,count(*)" ) ) ); fprintf($ fp,“%s \\ n”,implode(“,”,array_fill(0,5,“Code,count(*)”)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM