简体   繁体   中英

How to escape this mysql query in php script?

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);
}

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;

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