为什么我通过php / mysql生成的CSV文件上写有错误而不是数据

时间:2014-12-09 03:51:12

标签: php mysql csv

我一直试图通过我的一个sql表(innoDB)创建CSV文件。目前,当我运行生成CSV的相应脚本时。它会下载一个CSV文件,该文件在其单元格上写入错误,如下所示:

  

PHP警告:空行数据包正文

脚本:

$host = 'localhost'; // MYSQL database host adress
$db = 'solodb'; // MYSQL database name
$user = 'root'; // Mysql Datbase user
$pass = ''; // Mysql Datbase password

$link = mysql_connect($host, $user, $pass);
mysql_select_db($db);

require 'exportcsv.inc.php';

$table="report"; // this is the tablename that i want to export to csv from mysql.

exportMysqlToCsv($table);

生成CSV的功能:

function exportMysqlToCsv($table,$filename = 'export.csv')
{
    $csv_terminated = "\n";
    $csv_separator = ",";
    $csv_enclosed = '"';
    $csv_escaped = "\\";
    $sql_query = "select * from $table";


// Gets the data from the database
$result = mysql_query($sql_query);
$fields_cnt = mysql_num_fields($result);


$schema_insert = '';

for ($i = 0; $i < $fields_cnt; $i++)
{
    $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
        stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
    $schema_insert .= $l;
    $schema_insert .= $csv_separator;
} // end for

$out = trim(substr($schema_insert, 0, -1));
$out .= $csv_terminated;

// Format the data
while ($row = mysql_fetch_array($result))
{
    $schema_insert = '';
    for ($j = 0; $j < $fields_cnt; $j++)
    {
        if ($row[$j] == '0' || $row[$j] != '')
        {

            if ($csv_enclosed == '')
            {
                $schema_insert .= $row[$j];
            } else
            {
                $schema_insert .= $csv_enclosed . 
                str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
            }
        } else
        {
            $schema_insert .= '';
        }

        if ($j < $fields_cnt - 1)
        {
            $schema_insert .= $csv_separator;
        }
    } 

    $out .= $schema_insert;
    $out .= $csv_terminated;
}

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: " . strlen($out));
// Output to browser with appropriate mime type, you choose ;)
header("Content-type: text/x-csv");
//header("Content-type: text/csv");
//header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=$filename");
echo $out;
exit;
}

更新 当表有超过10000条记录时会出现此错误,否则它可以正常工作。任何人都可以告诉为什么这对于大量的记录感到满意

2 个答案:

答案 0 :(得分:0)

这是一个我用来向csv获取数据的小型库,它在过去运行得很好。它使用fputcsv(),但它需要PDO数据库连接(这可能没问题,因为您使用的数据库连接已弃用)。 FetchTable()函数是实际构建csv并将其保存到磁盘的函数,因此,您可以复制该代码并将其修改为普通过程:

class   CSVtoZip
    {
        protected   $db;
        protected   $files;
        protected   $zipname;
        protected   $headerAddr;
        protected   $rootdir;

        public  function __construct($rootdir = '',$db)
            {
                $this->db       =   $db;
                $this->rootdir  =   $rootdir;
            }

        public  function FetchTable($table = false, $columns = array(), $filename = false, $db)
            {
                if($table !== false && $filename !== false && !empty($columns)) {

                        $filename   =   $this->rootdir.$filename.".csv";
                        $output     =   fopen($filename, 'w');
                        fputcsv($output, $columns);

                        $query      =   $this->db->prepare("select ".implode(",",$columns)." from $table");
                        $query->execute();

                        while($rows = $query->fetch(PDO::FETCH_ASSOC)) {
                                fputcsv($output, $rows);
                            }

                        $this->files[]  =   $filename;
                        fclose($output);
                    }
            }

        public  function Zipit($zipname = false)
            {
                if(isset($this->files) && !empty($this->files)) {
                        $this->zipname  =   ($zipname == false)? date("YmdHis").uniqid().".zip":$zipname;
                        $zipper         =   new ZipArchive();
                        $zipper->open($this->rootdir.$this->zipname, ZipArchive::CREATE);

                        foreach($this->files as $filelocation) {
                                $zipper->addFile(basename($filelocation));
                            }

                        $zipper->close();

                        foreach($this->files as $filelocation) {
                                unlink($filelocation);
                            }

                        $this->headerAddr   =   "Location: ".str_replace($_SERVER['DOCUMENT_ROOT'],"",$this->rootdir.$this->zipname);
                        $this->DownloadZip();
                        unlink($this->rootdir.$this->zipname);
                    }
            }

        protected   function DownloadZip()
            {
                if(isset($this->headerAddr)) {
                        header('Content-Type: text/csv; charset=utf-8');
                        header('Content-Disposition: attachment; filename='.basename($this->zipname));
                        header($this->headerAddr);
                    }
            }
    }

// Accepts a root directory and database connection (PDO)
$csv    =   new CSVtoZip($_SERVER['DOCUMENT_ROOT'].'/mydirectory/',$con);

// Fetch 'users' table and grab specific columns, name the file users.csv
$csv->FetchTable('users',array('ID','usergroup','first_name'),'users');

// Fetch another table with 2 columns, name it table2.csv
$csv->FetchTable('table2',array('ID','username'),'table2');

// Zip the file(s) into an archive called tester.zip, then download it 
$csv->Zipit('tester.zip');

答案 1 :(得分:0)

这是一个表锁定与行锁定处理巨大的记录/缓冲区和更新。

解决这个问题的最简单方法就是将其切换到InnoDB。

在执行此操作之前锁定MyISAM表也可能有效,请给我们一点。

编辑:我发现,只是尝试锁定它,这样你就是唯一一个在你转储时访问它的人。