0

When I try using something like

$aSqlCommand = "SELECT ............. '\n'";

The quote continues on after the double quote, now that's probably because it's an escape character right? How can I change it so the quote will stop?

I want to use the LINES TERMINATED BY '\n' in it.

Full Query:

$sql2= "SELECT date,value,domain
INTO OUTFILE 'export1.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'";

From: How to output MySQL query results in CSV format? and http://www.tech-recipes.com/rx/1475/save-mysql-query-results-into-a-text-or-csv-file/

Community
  • 1
  • 1
Strj500
  • 49
  • 3
  • 5
    Why do you need a `\n` in your query ? – Raptor Apr 15 '14 at 03:35
  • Can you provide your whole query? – Logan Wayne Apr 15 '14 at 03:36
  • I second this, there's no need to prettify the query. Is it perhaps within data you're trying to insert? – ScottMcGready Apr 15 '14 at 03:36
  • To use LINES TERMINATED BY – Strj500 Apr 15 '14 at 03:36
  • please post the full querry –  Apr 15 '14 at 03:41
  • Does `\n` really need to be present when handling strings? Why not replace it with something safe like `str_replace("\n", ' nlb ', $str)` to later replace back into a line break like `str_replace(' nlb ', "\n", $str)` if you have to use it. Assuming no one would ever write " nlb " you are save to use that as a key. – WASasquatch Apr 15 '14 at 03:48
  • We need to see your full query in order to make an informed decision but the likelihood is you've missed out an ' somewhere in the query. – ScottMcGready Apr 15 '14 at 03:49
  • The full query is up there and the links from where basically it came. – Strj500 Apr 15 '14 at 03:50
  • @Strj500 Try my method. `TERMINATE BY ' nlb '` and then when displaying the code or writing it to your CSV you simply run the replacement to transform back into a line break. – WASasquatch Apr 15 '14 at 03:52
  • There must be a simple way to negate the '\' character to allow it's use in this MySQL query. Any suggestions? – Strj500 Apr 15 '14 at 03:56

2 Answers2

0

You need to escape \ and " as following: $sql2 = "SELECT date,value,domain INTO OUTFILE 'export1.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\n'";

vooD
  • 2,881
  • 2
  • 25
  • 34
-1

You need to escape the " from ENCLOSED BY and \n from LINES TERMINATED BY, just like this:

"SELECT date,value,domain INTO OUTFILE 'export1.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\n'"
rogcg
  • 10,451
  • 20
  • 91
  • 133
Thiago França
  • 1,817
  • 1
  • 15
  • 20