gpt4 book ai didi

php - 如何在php脚本中转义这个mysql查询?

转载 作者:行者123 更新时间:2023-11-29 22:46:19 25 4
gpt4 key购买 nike

该查询直接在 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

我需要在 php 脚本中使用它。我试图逃避特殊字符。但我无法让它发挥作用。这是脚本:

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

最佳答案

这是我经常使用的技巧:

$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;

关于php - 如何在php脚本中转义这个mysql查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29097721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com