mysqldump-to-csv
mysqldump-to-csv copied to clipboard
incorrect handling of single/double quotes and newlines
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:
- First entry should be without surrounding single quotes.
- 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.