0

This query works directly in mysql:

LOAD DATA LOCAL INFILE '/var/www/html/parsecsv/test.csv'
INTO TABLE test
COLUMNS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES

I'm need to use it in a php script. I'm trying to escape the special characters. But I can't get it to work. Here's the script:

$query = "LOAD DATA LOCAL INFILE '/var/www/html/parsecsv/test.csv'
INTO TABLE test
COLUMNS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
ESCAPED BY '\"'
LINES TERMINATED BY '\\n'
IGNORE 1 LINES";
$link = mysqli_connect("localhost", "wptest", "p@ssw0rd");
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
if (!mysqli_select_db($link, 'wptest')) {
    echo 'Could not select database';
    exit;
}
/* Select queries return a resultset */
if ($result = mysqli_query($link, $query)) {
    echo "It's working";
    /* free result set */
    mysqli_free_result($result);
}
anwarabq
  • 128
  • 2

1 Answers1

0

Here is the trick I use often:

$query = <<<QUERY
LOAD DATA LOCAL INFILE '/var/www/html/parsecsv/test.csv'
INTO TABLE test
COLUMNS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
QUERY;
Alex
  • 16,739
  • 1
  • 28
  • 51