php fputcsv 不工作
php fputcsv not working
我不明白为什么我的 fputcsv
不起作用。这是我得到的:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$id = $_GET['Id'];
include('DBConn.php');
$query = $conn->prepare('SELECT QName, tsql from pmdb.QDefs WHERE Id = ' . $id);
$query->execute();
$qdef = $query->fetch(PDO::FETCH_ASSOC);
// Create and open file for writing
$filepath = 'exports/';
$filename = $qdef['QName'] . '.csv';
//$filename = $qdef['QName'] . '.csv';
try
{
$openFile = fopen($filepath . $filename,'w');
header('Content-Encoding: UTF-8');
header('Content-Type: text/csv; charset:UTF-8');
header('Content-Disposition: attachment; filename="' . $filename . '"');
}
catch(Exception $e)
{
echo "Something went wrong<br>";
die( print_r( $e->getMessage()));
}
// Use returned tsql field as query for dataset
$tsql = $qdef['tsql'];
//echo "tsql<br>"; print_r($tsql); //This was to make sure that I'm getting the correct query
$query = $conn->prepare($tsql);
$query->execute();
// Output data to CSV file
$headers = NULL;
while ($row = $query->fetch(PDO::FETCH_ASSOC))
{
//Write column headings to file
if (is_null($headers))
{
$headers = array_keys($row);
if ($headers[0] == 'ID')
$headers[0] = 'Id';
fputcsv($openFile, $headers); //This is doing nothing The headers from here don't get printer to the file
//print_r($headers); //This was to check and see if the headers exist
print_r($openFile);echo ","; //This is to see what is returned from the fopen -> it returns "Resource is #4"
foreach($headers as $Header)
{
echo $Header. ","; //This was to print the headers to see if I can write to the file. I can the headers print.
}
}
//Write data
$modRow = preg_replace('/ \d{2}:\d{2}:\d{2}\.\d{3}/', '', $row);
/*
$modRow = str_replace('\r\n', " ", $modRow);
$modRow = str_replace('\n\r', " ", $modRow);
$modRow = str_replace('\n', " ", $modRow);
$modRow = str_replace('\r', " ", $modRow);
*/
fputcsv($openFile, $modRow, ',','"');//print_r($modRow); //The rows don't get added to the file from here like they should, but I also don't get any error.
}
// Close file
fclose($openFile);
//echo $filepath . $filename;
?>
没有错误 它只是导出一个空白文件,尽管它确实正确命名了它。大多数报告的文件应包含 30 多列和 10000 多行。
我的错误报告是这样设置的:
error_reporting(E_ALL|E_STRICT);//For use in trouble shooting
ini_set('display_errors', 'On');//For use in trouble shooting
我最终创建了一个解决方法,因为 In 无法使 fputcsv
工作。
我现在这样做:
foreach($modRow as $RowPrint)
{
echo '"' .trim(unserialize(serialize($RowPrint))). '"' .$sep;
}
echo $br;
其中 $sep = ","
和 $br = "\r\n"
我不明白为什么我的 fputcsv
不起作用。这是我得到的:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$id = $_GET['Id'];
include('DBConn.php');
$query = $conn->prepare('SELECT QName, tsql from pmdb.QDefs WHERE Id = ' . $id);
$query->execute();
$qdef = $query->fetch(PDO::FETCH_ASSOC);
// Create and open file for writing
$filepath = 'exports/';
$filename = $qdef['QName'] . '.csv';
//$filename = $qdef['QName'] . '.csv';
try
{
$openFile = fopen($filepath . $filename,'w');
header('Content-Encoding: UTF-8');
header('Content-Type: text/csv; charset:UTF-8');
header('Content-Disposition: attachment; filename="' . $filename . '"');
}
catch(Exception $e)
{
echo "Something went wrong<br>";
die( print_r( $e->getMessage()));
}
// Use returned tsql field as query for dataset
$tsql = $qdef['tsql'];
//echo "tsql<br>"; print_r($tsql); //This was to make sure that I'm getting the correct query
$query = $conn->prepare($tsql);
$query->execute();
// Output data to CSV file
$headers = NULL;
while ($row = $query->fetch(PDO::FETCH_ASSOC))
{
//Write column headings to file
if (is_null($headers))
{
$headers = array_keys($row);
if ($headers[0] == 'ID')
$headers[0] = 'Id';
fputcsv($openFile, $headers); //This is doing nothing The headers from here don't get printer to the file
//print_r($headers); //This was to check and see if the headers exist
print_r($openFile);echo ","; //This is to see what is returned from the fopen -> it returns "Resource is #4"
foreach($headers as $Header)
{
echo $Header. ","; //This was to print the headers to see if I can write to the file. I can the headers print.
}
}
//Write data
$modRow = preg_replace('/ \d{2}:\d{2}:\d{2}\.\d{3}/', '', $row);
/*
$modRow = str_replace('\r\n', " ", $modRow);
$modRow = str_replace('\n\r', " ", $modRow);
$modRow = str_replace('\n', " ", $modRow);
$modRow = str_replace('\r', " ", $modRow);
*/
fputcsv($openFile, $modRow, ',','"');//print_r($modRow); //The rows don't get added to the file from here like they should, but I also don't get any error.
}
// Close file
fclose($openFile);
//echo $filepath . $filename;
?>
没有错误 它只是导出一个空白文件,尽管它确实正确命名了它。大多数报告的文件应包含 30 多列和 10000 多行。
我的错误报告是这样设置的:
error_reporting(E_ALL|E_STRICT);//For use in trouble shooting
ini_set('display_errors', 'On');//For use in trouble shooting
我最终创建了一个解决方法,因为 In 无法使 fputcsv
工作。
我现在这样做:
foreach($modRow as $RowPrint)
{
echo '"' .trim(unserialize(serialize($RowPrint))). '"' .$sep;
}
echo $br;
其中 $sep = ","
和 $br = "\r\n"