Php 如何在查询中使用fgetcsv?

Php 如何在查询中使用fgetcsv?,php,sql,csv,Php,Sql,Csv,我有一个csv文件存储在另一个站点中。 我构建了一个简单的查询,我想将第一列和第二列插入到我的表中 <?php $fin = fopen('http://diederichs.com/reseller/RZZAQZYIJxPY6JxO/supply/csv','r') or die('cant open file'); try { $link = new PDO('mysql:dbname=;host=localhost', '', ''); echo 'Connectio

我有一个csv文件存储在另一个站点中。 我构建了一个简单的查询,我想将第一列和第二列插入到我的表中

<?php
$fin = fopen('http://diederichs.com/reseller/RZZAQZYIJxPY6JxO/supply/csv','r') or die('cant open file');
try {
    $link = new PDO('mysql:dbname=;host=localhost', '', '');
    echo 'Connection succeeded <br />' . PHP_EOL;
    $stmt = $db->prepare('INSERT INTO supply SET COL2 = :sku, reference = :reference');
    //Only give the length parameter if you **KNOW** that no line will be longer than that
    while (($data=fgetcsv($fin,1000,","))!==FALSE) {
        if ($stmt->execute(array(':sku' => $data[1], ':reference' => $data[0]))) {
            echo 'Insert ok<br />' . PHP_EOL;
        } else {
            $err = $stmt->errorInfo();
            echo 'Insert Failed: ' . $err[2] . PHP_EOL;
        }
    }
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
fclose($fin);
?>
当我进入我的php页面时,我可以看到消息“ConnectionSuccessed”,这很好,但我没有任何消息,比如“Insert failed”之类的

没有在我的表中进行插入

<?php
$fin = fopen('http://diederichs.com/reseller/RZZAQZYIJxPY6JxO/supply/csv','r') or die('cant open file');
try {
    $link = new PDO('mysql:dbname=;host=localhost', '', '');
    echo 'Connection succeeded <br />' . PHP_EOL;
    $stmt = $db->prepare('INSERT INTO supply SET COL2 = :sku, reference = :reference');
    //Only give the length parameter if you **KNOW** that no line will be longer than that
    while (($data=fgetcsv($fin,1000,","))!==FALSE) {
        if ($stmt->execute(array(':sku' => $data[1], ':reference' => $data[0]))) {
            echo 'Insert ok<br />' . PHP_EOL;
        } else {
            $err = $stmt->errorInfo();
            echo 'Insert Failed: ' . $err[2] . PHP_EOL;
        }
    }
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
fclose($fin);
?>

您正在使用更新语法,因为INSERT是

INSERT INTO supply(COL2,reference) VALUES(:sku,:reference)