mysqldump-to-csv icon indicating copy to clipboard operation
mysqldump-to-csv copied to clipboard

incorrect handling of single/double quotes and newlines

Open cronfy opened this issue 8 years ago • 0 comments

Script produces incorrect csv when parsing dump with single/double quotes and newlines. Example:

INSERT INTO `test` VALUES ('single quote \' test','double quote \" test','newline \n test');

After converting we have:

'single quote ' test',"double quote "" test",newline n test

Newline is obviously disappeared, and if we parse it with php fgetcsv(), we'll see incorrect single quotes:

$f = fopen('x.csv', 'r');
$a = fgetcsv($f);

foreach ($a as $v) {
        echo "[$v]\n";
}
$ php ./test.php 
['single quote ' test']
[double quote " test]
[newline n test]

Errors here:

  1. First entry should be without surrounding single quotes.
  2. Third entry must contain newline instead of just "n".

Expected result

single quote ' test,"double quote "" test","newline
test"

Note that there should be 2 lines of text for single csv entry, containing newline. First entry (with single quote) isn't quoted at all. I created this CSV with LibreOffice Calc, and it is parsed with php correctly.

cronfy avatar Mar 17 '16 08:03 cronfy