Correct query
Your CSV has rows delimited by three slashes and carriage return. Just use LINES TERMINATED BY "///\r"
in your query and it should work. At least it works for me on my MySQL 5.1.49.
LOAD DATA LOCAL INFILE "temp/test.csv"
INTO TABLE test
FIELDS TERMINATED BY ","
LINES TERMINATED BY "///\r"
IGNORE 1 LINES (one, two, three, four, five)
Debugging unprintable characters
When in doubt about what the unprintable characters are, you can always look at hexdump and decode the characters manually. Usually these characters are ASCII. On *NIXes, see man ascii
, otherwise look the table up on Internet (e.g. http://man-ascii.com/).
In MySQL, hexdump of a string is obtained by HEX()
function application, e.g.
SELECT HEX(one) FROM test
In POSIX shell, hexdump is generated using
od -t x1 temp/test.csv
If you do not want to be that hardcore, for common whitespace it is enough to convert it to C escape sequences (e.g. \r
for carriage return) using
od -c temp/test.csv
Maybe even simpler (but generally harder to explain) approach to identifying EOL type is just using the text editor of your choice and letting it tell you. CSV is a plain text format (in contrast to binary format). E.g. in Vim open the file and run se ff?
. This will show you fileformat=<EOL-type>
where <EOL-type>
is
unix
for\n
dos
for\r\n
mac
for\r
Beware multiple escaping levels
Notice that at the beginning of my answer I wrote just the query, not PHP command to execute it. In SQL, some characters are special and need to be escaped to be taken literally (quote inside string), other gain their special meaning through escaping (n
in \n
). In PHP, the SQL query must be placed in string, where another level of escaping takes place (and for different characters!). Another level of escaping comes e.g. when you call PHP from shell as php -r '…'
.
If you want to be sure that you got the escaping right, print the query before sending it to database.
$query = '…';
print $query;
#$result = mysql_query($query);
mysql
API was deprecated in PHP 5.5, removed in PHP 7.0
The old mysql
API (mysql_*
functions) was deprecated in PHP 5.5 and removed in PHP 7.0, it will not be available in future versions. Consult the PHP manual for choice of MySQL API. See also other relevant answers.