所以我在我的服务器上运行这个脚本,需要它用我的CSV文件中的数据替换SQL表中的所有数据:
<?php
$databasehost = "localhost";
$databasename = "import";
$databasetable = "import";
$databaseusername="import";
$databasepassword = "password";
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "test.csv";
if(!file_exists($csvfile)) {
die("File not found. Make sure you specified the correct path.");
}
try {
$pdo = new PDO("mysql:host=$databasehost;dbname=$databasename",
$databaseusername, $databasepassword,
array(
PDO::MYSQL_ATTR_LOCAL_INFILE => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
)
);
} catch (PDOException $e) {
die("database connection failed: ".$e->getMessage());
}
$affectedRows = $pdo->exec("
LOAD DATA LOCAL INFILE ".$pdo->quote($csvfile)." REPLACE INTO TABLE `$databasetable`
FIELDS TERMINATED BY ".$pdo->quote($fieldseparator)."
LINES TERMINATED BY ".$pdo->quote($lineseparator));
echo "Loaded a total of $affectedRows records from this csv file.\n";
?>
我已经添加了REPLACE INTO TABLE,希望它能替换它,但它只是在桌面上堆叠。
知道我可能做错了吗?
谢谢!