0

I am trying to export sql query result to csv file it works but the out csv data does not look good and strange.

MySQL Table Download Link: https://www.dropbox.com/s/vtr215bcxqo3wsy/data.sql?dl=0

CSV Generated by sql query:

Download Link to original Generated CSV File: https://www.dropbox.com/s/fnjf7ycmh08hd22/data.csv?dl=0

I am using following code:

$query = <<<EOL
 SELECT * FROM data  ORDER BY  FN ASC limit 3 
 INTO OUTFILE 'folder/data.csv'
 FIELDS TERMINATED BY ','
 OPTIONALLY ENCLOSED BY '"'
 LINES TERMINATED BY '\n'
EOL;

$result = mysql_query($query);

Why csv format is looking so weird and unacceptable? If I try same code for some other table then everything works like charm then what's wrong?

6
  • 2
    your fields have line breaks in them. and since you've specified that lines are terminated by \n, any \n inside your fields terminate that "row". Commented Jul 31, 2015 at 14:36
  • Could you provide the actual text file that is created and not just how it looks when imported into a spreadsheet? Commented Jul 31, 2015 at 14:37
  • @Terry Just added generated file. please check Commented Jul 31, 2015 at 14:45
  • @MarcB that makes sense. But can you please guide me that how can I overcome this issue? Commented Jul 31, 2015 at 14:46
  • try fields escaped by '\\', see if that escapes the literal \n into \\n. otherwise you'll probably need str_replace and convert the linebreaks into something else (maybe html <br>?) Commented Jul 31, 2015 at 14:50

3 Answers 3

1

See final answer below

It looks like your lines are terminated by \\n and it is throwing the extra slashes in random places.

Instead try a double slash followed by an n (\\n) and see what happens:

$query = <<<EOL
 SELECT * FROM data  ORDER BY  FN ASC limit 3 
 INTO OUTFILE 'folder/data.csv'
 FIELDS TERMINATED BY ','
 OPTIONALLY ENCLOSED BY '"'
 LINES TERMINATED BY '\\n'
EOL;

$result = mysql_query($query);

EDIT

Final Answer

Another observation: I noticed that in your PROP_TYPE field, there are \r\n characters. Is there any way you can filter them out in your query using the REPLACE() function?

I know you are looking for a solution that is SQL based, and this is a hard issue because of the massive amount of data. Hope this leads you to the correct solution.

As you mentioned, using update data set PROP_TYPE = replace(PROP_TYPE, '"','') fixed the issue.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your reply. I tried this but still did not work
Does the file look any different? It is difficult to test for sure without data from the actual database (which is not really possible for us).
Please download database (3 rows) dropbox.com/s/vtr215bcxqo3wsy/data.sql?dl=0
Thanks @Terry for your efforts. I just posted answer myself to this question below so please have a look :)
1

Consider simply using PHP to connect to MySQL, run query, then output to csv.

<?php

$host="localhost";
$username="user";
$password="password";
$database="dbName";

# open connection
try {
    $dbh = new PDO("mysql:host=$host;dbname=$database",$username,$password);
}
catch(PDOException $e) {  
    echo $e->getMessage();  
}
$sql = "SELECT * FROM data ORDER BY FN ASC limit 3;";
$STH = $dbh->query($sql);    

$STH->setFetchMode(PDO::FETCH_ASSOC);  

while($row = $STH->fetch()) {
    # write to csv file
    $fs = fopen("folder/data.csv","a");
        fputcsv($fs, $row);
    fclose($fs);    
}

# close connection
$dbh = null;

?>

3 Comments

Thanks for great response. This is good but not perfect for 10 million rows. this would be very slow.
Yet your query limits to 3? Also, why would you need to export this many rows to a flatfile? If exporting to another system why not connect to ODBC?
Well 3 is just for example and quick testing. And I need to import in flatfile because it is my client's requirement.
1

Finally I fixed my issue.

Actually @Terry is right. There was some issue with a field PROP_TYPE in table. PROP_TYPE field had double quotes " in its values that was causing issue. For example

PROP_TYPE

"Value 1"

"Value 2" ....

So first of all I had to remove extra double quotes using update data set PROP_TYPE = replace(PROP_TYPE, '"','') so now my issue is fixed.

Thanks all of you for your efforts.

I really appreciate.

1 Comment

Thanks for posting. If you would not mind, could you "accept" my updated answer by checking the checkmark beside it? This will give us both "points" on Stackoverflow and is a good way to show appreciation for the work put into answering a question. Glad you got a solution that worked!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.