I am trying to import a CSV into a MySQL table with a PHP script. This SQL command successfully imports the CSV file into the SQL table:
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
Now I want to convert this SQL command into a PHP script. Here's my attempt at writing the PHP script:
<?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");
?>
This script results in an error:
HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.
The errors log reads:
[18-Dec-2012 12:37:38] PHP Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /path/testconnect.php on line 13
The $Result variable is called on line 13, so the error must be therein.
I'm not sure what the cause of this error is. Any help is greatly appreciated!