简体   繁体   English

用PHP导出CSV文件

[英]Export CSV file in PHP

In exporting CSV file using PHP, there are some records that won't be included. 使用PHP导出CSV文件时,有些记录将不包括在内。 It seemed like while transferring, it stopped even if there are still records to be exported. 似乎在传输时,即使仍有记录要导出,它也停止了。 What's the problem? 有什么问题?

i have experience several error while exported CSV content to database. 将CSV内容导出到数据库时遇到了一些错误。 most of the problem is because database fail while save record. 大多数问题是因为数据库在保存记录时失败。 for example you have data like this : 例如您有这样的数据:

 "1;me;24"
 "2;you;"
 "3;they;26"
 "4;she;27"
 "5;it;28"

if your database have the constrain the row cannot be null, so the database will throw error to php. 如果您的数据库有约束条件,则该行不能为null,因此数据库将向php抛出错误。 and the php will stop loop before the all of record recorded to database. 并且php将在所有记录记录到数据库之前停止循环。

the solution, you can add exception at your code, when the database fail to insert to database, it will continue the loop until it finish recorded csv to your database. 解决方案中,您可以在代码中添加异常,当数据库无法插入数据库时​​,它将继续循环直到完成将csv记录到数据库中为止。

<?php

error_reporting(E_ERROR);

class MySQLException extends Exception
{
    public function __construct($message,$code=0){                
        parent::__construct($message,$code);
    }
}

class Mysql
{    
    private $_DB_HOST;
    private $_DB_USERNAME;
    private $_DB_PASSWORD;
    private $_DB_NAME;
    private $_DB_SELECT_STATE;

    private $_CONNECTION;

    public function __construct($host, $username, $password, $dbname) {
        $this->_DB_HOST = $host;
        $this->_DB_USERNAME = $username;
        $this->_DB_PASSWORD = $password;
        $this->_DB_NAME = $dbname;

        $this->__connect();
        $this->__select_db();
    }

    private function __connect() {
        $this->_CONNECTION = mysql_connect($this->_DB_HOST, $this->_DB_USERNAME, $this->_DB_PASSWORD);

        if(!$this->_CONNECTION) {            
            throw new MySQLException('Could Not Connect to database with given setting');            
        }
    }

    private function __select_db() {
        $this->_DB_SELECT_STATE = mysql_select_db( $this->_DB_NAME , $this->_CONNECTION);

        if(!$this->_DB_SELECT_STATE) {
            throw new MySQLException('Could Not select to database with given setting');
        }
    }

    public function query($query){
        if( ( $result = mysql_query($query, $this->_CONNECTION )  )) {
            return $result;
        } else {
            $command = explode(' ', trim($query));
            throw new MySQLException('Could not do' . $command . ' query');
        }

    }
}


// connect to database
$database = new Mysql("localhost", "username", "password", "test");

// example your csv file before parsed
$csv = array("1;me;24", "2;you;", "3;they;26", "4;she;27", "5;it;28");

for($i = 0; $i < sizeof($csv); $i++){
    try{
        $row = explode(";", $csv[$i]);        
        $database->query("INSERT INTO testtable (id, name, age) VALUES (".$row[0].", '".$row[1]."', ".$row[2].")");
    } catch (MySQLException $e){
        echo 'We could not insert to the database <br>';
    }
}

if you wasn't add any try catch, your code will stop while inserting the second query 如果您没有添加任何try catch,则在插入第二个查询时,代码将停止

"1;me;24"
"2;you;"
"3;they;26"
"4;she;27"
"5;it;28"

but the code remain loop till end parsing if you catch the exception. 但是如果您捕获到异常,代码将一直循环直到解析结束。

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

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