gpt4 book ai didi

php - 使用 PHP 按钮生成 CSV 时,CSV 文件中的内容附加两次

转载 作者:行者123 更新时间:2023-11-29 02:57:00 24 4
gpt4 key购买 nike

这是我生成 csv 文件的代码。当我单击 php 按钮生成 Csv 文件时,它会根据数据库中的类别列填充内容。但我的问题是当内容在csv 文件,如下所示。请帮助指出我必须修改代码的地方,以便我只能获得一次填充的内容,如下所示。提前致谢。

创建csv.php

<?php

$servername = "localhost";
$username = "user";
$password = "";
$dbname = "stats";

define("DB_SERVER", "localhost");
define("DB_NAME", "stats");
define("DB_USER", "user");
define("DB_PASSWORD", '');

$dbconn = @mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD);
$conn = @mysql_select_db(DB_NAME,$dbconn);


// Create connection
//$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
echo "DB connection failed";
}

// Query DB to fetch hit count for each category and in turn create corresponding .csv file
function createCSVFile($type) {
$msql = "SELECT TRIM(TRAILING '.000000' from UNIX_TIMESTAMP(hitdate)*1000) as unixdate,count from h_stats where category='".$type."' order by unixdate asc";

$query = mysql_query($msql);

$type = str_replace(' ', '', $type);

$tmp_file = "data/tmp_".$type.".csv";
$fp = fopen("$tmp_file", "w");

// Write the query contents to temp file
while($row = mysql_fetch_array($query))
{
fputcsv($fp, $row);

}
fclose($fp);

// Modify the contents of the file as per the high chart input data format
$fp = fopen("$tmp_file", 'r+');
rewind($fp);
$file = "data/".$type.".csv";


$final = fopen("$file", 'w');

while($line = fgets($fp)){
trim($line);
$line = '['.$line.'],';
fputs($final,$line);
}

// Append var $type and remove the trailing ,
$final = file_get_contents($file);
$content = 'var '.$type .'= [' . rtrim($final, ","). ']';
file_put_contents("$file",$content);

}

// Query DB to fetch success/failure count for Hits and in turn create corresponding .csv file
function createHitOutcomeCSVFile($type,$category) {
$sql = "SELECT TRIM(TRAILING '.000000' from UNIX_TIMESTAMP(hitdate)*1000) as unixdate,".$type." from h_stats where category='".$category."' order by unixdate asc";

$query = mysql_query($sql);

$tmp_file = "data/tmp_".$type."_".$category.".csv";
$fp = fopen("$tmp_file", "w");

// Write the query contents to temp file
while($row = mysql_fetch_array($query)){
fputcsv($fp, $row);
}
fclose($fp);

// Modify the contents of the file as per the high chart input data format
$fp = fopen("$tmp_file", 'r+');
rewind($fp);

$category = str_replace(' ', '', $category);

$file = "data/".$type."_".$category.".csv";

$final = fopen("$file", 'w');

while($line = fgets($fp)){
trim($line);
$line = '['.$line.'],';
fputs($final,$line);
}

// Append var $type and remove the trailing ,
$final = file_get_contents($file);
$content = 'var '.$type.'_'.$category.'= [' . rtrim($final, ","). ']';
file_put_contents("$file",$content);

}
// Invoke function to create the Hits.csv file
createCSVFile('Hits');

// Invoke function to get Three Hits csv file
createHitOutcomeCSVFile('TCount','Hits');

// Invoke function to get O2 Hits csv file
createHitOutcomeCSVFile('BCount','Login');

echo "Generated successfully";

?>

不是预期的包含两次填充数据的 csv 文件:

var Login_Hits= [[1427826600000,1427826600000,8763,8763
]]

按照 highcharts 格式的预期 csv 文件:

var Login_Hits= [[1427826600000,8763
]]

最佳答案

尝试调试它...
这比看到打字错误要容易...
看起来 tmp 文件已经损坏了...
尝试显示 $row 变量和 $query...
问题可能出在这里……

关于php - 使用 PHP 按钮生成 CSV 时,CSV 文件中的内容附加两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29741234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com