我正在尝试使用 PHP 脚本将 CSV 导入 MySQL 表。此 SQL 命令成功地将 CSV 文件导入 SQL 表:
mysql> LOAD DATA LOCAL INFILE 'property_re_1.csv'
-> REPLACE INTO TABLE `markers`
-> FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\'
-> LINES TERMINATED BY '\n' IGNORE 1 LINES;
Query OK, 315 rows affected (0.01 sec)
Records: 315 Deleted: 0 Skipped: 0 Warnings: 0
现在我想将此 SQL 命令转换为 PHP 脚本。这是我编写 PHP 脚本的尝试:
<?PHP
$dbhost = 'localhost';
$dbuser = 'myusername';
$dbpasswd = 'mypassword';
$db = "db_markers";
$dbh = mysql_connect($dbhost, $dbuser, $dbpasswd) or die("Unable to connect to SQL server");
$my_db = @mysql_select_db($db, $dbh) or die("Unable to select database");
$file = $_SERVER['DOCUMENT_ROOT']."/path/property_re_1.csv";
$result=mysql_query("LOAD DATA LOCAL INFILE '$file' REPLACE INTO TABLE `markers` FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\n' IGNORE 1 LINES");
?>
此脚本导致错误:
HTTP 错误 500(内部服务器错误):服务器尝试完成请求时遇到意外情况。
错误日志显示:
[18-Dec-2012 12:37:38] PHP Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /path/testconnect.php on line 13
$Result 变量在第 13 行被调用,所以错误一定在其中。
我不确定这个错误的原因是什么。任何帮助是极大的赞赏!