PHP语法字符串连接

时间:2012-08-08 09:07:11

标签: php mysql

    $a = '
        SELECT * FROM `'. $table .'` WHERE `chrDate` > "'. $lastDate .'" INTO OUTFILE "'. $file .'"
        FIELDS TERMINATED BY ", " ENCLOSED BY \'"\' ESCAPED BY "\"
        LINES TERMINATED BY "\n"
';

我不知道为什么我不能这样做,我试着解决它几次,但每次我都给出这个错误:(

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 '\n"' at line 2

* Echo $ a *

SELECT * FROM `tbl_SaleApa` WHERE `chrDate` > "2001/03/29" INTO OUTFILE "C:/xampp/htdocs/transfer/tempfile/tbl_SaleApa.sql"
FIELDS TERMINATED BY ", " ENCLOSED BY '"' ESCAPED BY "\"
LINES TERMINATED BY "\n"

4 个答案:

答案 0 :(得分:1)

我有这个: 编辑:您还将变量封装在双引号内,这是不需要的。查看更新的代码。

<?php

$table='myTable';
$lastDate='myDate';
$file='myFile.ext';
$a = "SELECT * FROM $table WHERE chrDate > '$lastDate' INTO OUTFILE '$file' 
    FIELDS TERMINATED BY ', ' ENCLOSED BY '\"' ESCAPED BY '\\' 
    LINES TERMINATED BY '\\n'"; 

echo $a;

?>

哪个输出:

SELECT * FROM myTable WHERE chrDate > 'myDate' INTO OUTFILE 'myFile.ext'
FIELDS TERMINATED BY ', ' ENCLOSED BY '"' ESCAPED BY '\' 
LINES TERMINATED BY '\n'

答案 1 :(得分:0)

$sql = '
SELECT * FROM `'. $table .'` WHERE `chrDate` > "'. $lastDate .'" INTO OUTFILE "'. $file .'"
FIELDS TERMINATED BY ", " ENCLOSED BY \'"\' ESCAPED BY "\\"
LINES TERMINATED BY "\n"';

答案 2 :(得分:0)

  $a = "SELECT * FROM " . $table . " WHERE chrDate > '" . $lastDate . "' INTO OUTFILE '" . $file . "' 
        FIELDS TERMINATED BY ', ' ENCLOSED BY '\"' ESCAPED BY '\\' 
        LINES TERMINATED BY '\n'"; 

答案 3 :(得分:0)

解决了我的问题:

$a = '
SELECT * FROM `'. $table .'` WHERE `chrDate` > "'. $lastDate .'" INTO OUTFILE "'. $file .'"
FIELDS TERMINATED BY ", " ENCLOSED BY \'"\' ESCAPED BY "\\\"
LINES TERMINATED BY "\n"
';

ESCAPED BY "\"更改为ESCAPED BY "\\\"