Php 使用PDO加载CSV文件会导致语法错误

Php 使用PDO加载CSV文件会导致语法错误,php,mysql,csv,pdo,Php,Mysql,Csv,Pdo,我需要将100多个CSV文件加载到MySQL中,所以我正在编写一个脚本来实现这一点。除此之外,我还有以下代码段,用于导入每个CSV文件: private function importFile(){ if($this->connection->beginTransaction()){ $transactionFailed = false; $importStatement = $this->connection->prepa

我需要将100多个CSV文件加载到MySQL中,所以我正在编写一个脚本来实现这一点。除此之外,我还有以下代码段,用于导入每个CSV文件:

    private function importFile(){
    if($this->connection->beginTransaction()){
        $transactionFailed = false;
        $importStatement = $this->connection->prepare("
            LOAD DATA INFILE :file
            REPLACE INTO TABLE :table;
        ");
        foreach($this->fetchFileList() as $file){
            $executeSuccesfull = $importStatement->execute(array(
                ':file' => $this->path . DIRECTORY_SEPARATOR . $file,
                ':table' => $file
            ));
            if(!$executeSuccesfull){
                $transactionFailed = true;
                $this->fetchDBError($importStatement);
                $this->connection->rollBack();
                break;
            }
        }

        if(!$transactionFailed){
            $this->connection->commit();
        }
    }
    else{
        $this->fetchDBError();
    }
}
$this->path
指向所有CSV文件所在的路径(我选中了)。每个
$file
与导入其数据的表具有完全相同的名称,没有文件扩展名(其中任何一个都没有
.csv
)。我使用的是在WindowsXP上运行的PHP5.3.9和MySQL 5.0.27。所有这些都在我的本地机器上运行

问题是,我遇到了以下错误:

[ERROR 42000] — You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near
''the_first_file'' at line 2 (1064)

The query issued was:

            LOAD DATA INFILE :file
            REPLACE INTO TABLE :table;
此代码由我的类的另一个方法生成:

    private function fetchDBError($statement = null){
    $errorCode = null;
    $errorInfo = null;
    $queryString = '';
    if($statement){
        $errorInfo = $statement->errorInfo();
        $errorCode = $statement->errorCode();
        $queryString = "<p>The query issued was:</p><pre>{$statement->queryString}</pre>";
    }
    else{
        $errorInfo = $this->connection->errorInfo();
        $errorCode = $this->connection->errorCode();
    }
    $this->success = false;
    $this->message = "<p>[ERROR $errorCode] {$errorInfor[0]} &mdash; {$errorInfo[2]} ({$errorInfo[1]})</p>$queryString";
私有函数fetchDBError($statement=null){
$errorCode=null;
$errorInfo=null;
$queryString='';
如果($报表){
$errorInfo=$statement->errorInfo();
$errorCode=$statement->errorCode();
$queryString=“发出的查询是:

{$statement->queryString}”;
      $filepath = $this->path . DIRECTORY_SEPARATOR .  str_replace("'", "''", $file);
} 否则{ $errorInfo=$this->connection->errorInfo(); $errorCode=$this->connection->errorCode(); } $this->success=false; $this->message=“[ERROR$errorCode]{$errorInfor[0]}&mdash;{$errorInfo[2]}({$errorInfo[1]})

$queryString”;
一个或多个文件包含口香糖,请尝试使用以下方法:

LOAD DATA INFILE '$filepath' INTO TABLE test
FIELDS TERMINATED BY ',';
然后在查询参数中使用$filepath

这是mysql加载数据函数的一个示例:

还可以尝试指定以结尾的字段


在stackoverflow的帖子中,我找到了关于prepare函数和LOAD DATA的内容。

还是一样。第一个文件没有任何撇号,也没有文件名。仍然没有更改。我尝试放置以“\t”结尾的
字段。
,它与我的CSV文件格式匹配,但没有任何改进。尝试在前后添加单引号参数:file(':file')PDO填充了准备语句中的所有单引号,因此当您尝试手动插入它们时,它会呈现一个错误。我还是尝试了:
Warning:PDOStatement::execute()[PDOStatement.execute]:SQLSTATE[HY093]:无效参数编号:绑定变量的数量与令牌的数量不匹配
请尝试阅读此帖子: