我从php报告网站导出到CSV。我的报告超过80k行。当我导出其中一个时,整个数据集不会被导出。我曾多次尝试使用88k +行的报告,并导出87k +行,然后在导出的最后一行中停止。
发生了什么事?从MSSQL中提取数据的查询是正确的(我已经检查过)。
这是我的导出文件:
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('DBConn.php');
include 'Helper/LogReport.php';
if(isset($_GET['Id']))
{
$id = $_GET['Id'];
$query = $conn->prepare('SELECT QName, tsql from pmdb.QDefs WHERE Id = ' . $id);
$query->execute();
$qdef = $query->fetch(PDO::FETCH_ASSOC);
}
else
{
$query = $conn->prepare("SELECT QName, tsql from pmdb.QDefs WHERE QName = '" .$TableName. "'");
$query->execute();
$qdef = $query->fetch(PDO::FETCH_ASSOC);
}
// Create and open file for writing
$filepath = 'exports/';
$filename = $qdef['QName'] . '.csv';
try
{
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()));
}
//define separators
$sep = ","; //separator
$br = "\r\n"; //line break
// Use returned tsql field as query for dataset
$tsql = $qdef['tsql'];
if(isset($DataReturn))
{
if(strpos($DataReturn['Order'],'EDIT'))
{
$DataReturn['Order'] = str_replace('EDIT','Id',$DataReturn['Order']);
}
$tsql = $tsql . $DataReturn['WhereClause'] . $DataReturn['Order'] . $DataReturn['Limit'];
}
$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';
foreach($headers as $Header)
{
echo $Header. ",";
}
echo $br;
}
//Write data
$modRow = preg_replace('/ \d{2}:\d{2}:\d{2}\.\d{3}/', '', $row);
$modRow = preg_replace( "/\r|\n/", "", $modRow );
foreach($modRow as $RowPrint)
{
echo '"' .trim(unserialize(serialize($RowPrint))). '"' .$sep;
}
echo $br;
}
我没有看到任何阻止数据流完成导出的内容。
修改
我尝试过这样的fputcsv()
方法:
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('DBConn.php');
include 'Helper/LogReport.php';
//print_r($_GET);
if(isset($_GET['Id']))
{
$id = $_GET['Id'];
$query = $conn->prepare('SELECT QName, tsql from pmdb.QDefs WHERE Id = ' . $id);
$query->execute();
$qdef = $query->fetch(PDO::FETCH_ASSOC);
}
else
{
$query = $conn->prepare("SELECT QName, tsql from pmdb.QDefs WHERE QName = '" .$TableName. "'");
$query->execute();
$qdef = $query->fetch(PDO::FETCH_ASSOC);
}
// Create and open file for writing
$filepath = 'exports/';
$filename = $qdef['QName'] . '.csv';
try
{
$openFile = fopen($filepath . $filename,'a');
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()));
}
//define separators
$sep = ","; //separator
$br = "\r\n"; //line break
// Use returned tsql field as query for dataset
$tsql = $qdef['tsql'];
if(isset($DataReturn))
{
if(strpos($DataReturn['Order'],'EDIT'))
{
$DataReturn['Order'] = str_replace('EDIT','Id',$DataReturn['Order']);
}
$tsql = $tsql . $DataReturn['WhereClause'] . $DataReturn['Order'] . $DataReturn['Limit'];
}
$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);
}
//Write data
$modRow = preg_replace('/ \d{2}:\d{2}:\d{2}\.\d{3}/', '', $row);
$modRow = preg_replace( "/\r|\n/", "", $modRow );
fputcsv($openFile, $modRow, ',','"');//print_r($modRow);
foreach($modRow as $RowPrint)
{
error_log(date("Y/m/d h:i:sa "). ' RowPrint: ' .$RowPrint. '\n',3,'C:\Temp\LogPHP.txt');
}
echo $br;
}
// Close file
fclose($openFile);
这只会创建一个空文件。
更新
我为一个应该有103k +行的报告运行了几个报告。它们都停在61000到62000行之间。我做了一个字符数,他们都有大约4000万。我没有看到任何关于4000万字符限制的内容。
答案 0 :(得分:0)
我让它工作并想出如何使fputcsv
起作用。
我需要在导出结束时添加exit()
。现在,它不仅可以发送所有数据fputcsv
。我从another question得到了关于让fputcsv
工作的答案。