PHP/MYSQL - 通过 as php 变量进行 mysql 查询相关的语法错误

发布于 2024-12-14 10:44:19 字数 458 浏览 0 评论 0原文

我在以下变量中遇到语法错误问题:

$uploadQuery = "
LOAD DATA LOCAL INFILE '".$docRoot."/../../includes/dbUploads/".$fileToUpload."' 
INTO TABLE `promotions` 
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' 
ESCAPED BY '\\' 
LINES TERMINATED BY '\r\n'  
";

我知道它与 LOAD DATA... 行中 ' 字符的转义有关。但当谈到问题到底是什么,或者如何以正确的方式重写这个查询时,我感到很困惑。

所以,我的问题是:

我如何以正确的方式重写所述变量,以免出现与之相关的语法错误。

如果有人对此有任何建议或意见,我们将不胜感激。

谢谢你!

I am having a problem with syntax errors in the following variable:

$uploadQuery = "
LOAD DATA LOCAL INFILE '".$docRoot."/../../includes/dbUploads/".$fileToUpload."' 
INTO TABLE `promotions` 
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' 
ESCAPED BY '\\' 
LINES TERMINATED BY '\r\n'  
";

I know that it has something do do with the escaping of the ' characters in the LOAD DATA... line. But I am stumped when it comes to what exactly the problem is, or how to reword this query in the correct manner.

So, My question is this:

How do i reword the stated variable in the correct manner, as to have no syntax errors relating to it.

If anyone has any suggestions or input with this, it would be greatly appreciated.

Thank You!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里寻她 2024-12-21 10:44:19

缺少转义字符,这是正确的字符串:

$uploadQuery = "
LOAD DATA LOCAL INFILE '".$docRoot."/../../includes/dbUploads/".$fileToUpload."' 
INTO TABLE `promotions` 
FIELDS TERMINATED BY ',' 
ENCLOSED BY '\"' 
ESCAPED BY '\\' 
LINES TERMINATED BY '\\r\\n'  
";

请注意第 5 行额外的 \。它将 " 视为字符串终止符。另一个问题(不会导致语法错误)位于第 7 行,您需要转义反斜杠。PS

标记分析器甚至选择了它up :P

编辑:您可能还需要将第 7 行更改为 ESCAPED BY '\\\\' 因为在 PHP 解析它之后这会减少为 ESCAPED BY '\\'

Missing an escape character, this is the correct string:

$uploadQuery = "
LOAD DATA LOCAL INFILE '".$docRoot."/../../includes/dbUploads/".$fileToUpload."' 
INTO TABLE `promotions` 
FIELDS TERMINATED BY ',' 
ENCLOSED BY '\"' 
ESCAPED BY '\\' 
LINES TERMINATED BY '\\r\\n'  
";

Note the extra \ on the 5th line. It was treating the " as a string terminator. Also another problem (that doesn't cause a syntax error) is on the 7th line, you need to escape the backslashes.

P.S. the markup analyzer even picked it up :P

Edit: you probably also need to change line 7 to ESCAPED BY '\\\\' since this reduces to ESCAPED BY '\\' after PHP parses it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文