【发布时间】:2014-06-17 03:02:50
【问题描述】:
我有一个存储在另一个站点中的 csv 文件。 我建立了一个简单的查询,我想将第一列和第二列插入到我的表中。
当我进入我的 php 页面时,我可以看到消息“连接成功”,这很好,但我没有任何消息,例如“插入失败”或其他什么。
我的表中没有插入。
<?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);
?>
【问题讨论】: