简体   繁体   中英

MySQL: You have an error in your SQL syntax; near ''' at line 5

I'm running this query in a *.php file and receive the following error:

LOAD DATA INFILE 'Employees.txt'
INTO TABLE employees
FIELDS TERMINATED BY ';'
ENCLOSED BY '\"'
ESCAPED BY '\\'
LINES TERMINATED BY '\r'

Response:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''' at line 5

I am running mysql version 5.6.17 and php version 5.5.12
Text file simple has:

"000";"Employee Name"

for each line.
The query works fine when used within phpmyadmin or via mysql console.
PHP Code:

<?php
date_default_timezone_set('America/New_York');
###### db ##########
$db_username = 'user';
$db_password = 'pass';
$db_name = 'dbase';
$db_host = 'localhost';
################

$query="
LOAD DATA INFILE 'Employees.txt'
INTO TABLE employees
FIELDS TERMINATED BY ';'
ENCLOSED BY '\"'
ESCAPED BY '\\'
LINES TERMINATED BY '\r'
";
echo $query.'<br>';

//Connect to DB
    $connecDB = mysqli_connect($db_host, $db_username, $db_password,$db_name)
    or die('could not connect to database');
    echo "Connected to MySQL<br>";

mysqli_query($connecDB,$query) or die(strftime('%c')." ".mysqli_error($connecDB));
mysqli_close();
echo strftime('%c')." ok!";

?>

Try this way:

$query="
LOAD DATA LOCAL INFILE 'Employees.txt'
INTO TABLE employees
FIELDS TERMINATED BY ';'
ENCLOSED BY '\\\"'
ESCAPED BY '\\\\'
LINES TERMINATED BY '\\r'
";
echo $query.'<br>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM