Answer the question
In order to leave comments, you need to log in
Missing character \ when parsing csv?
I have a csv file in which some fields contain the \ character.
This file is parsed using php and the resulting data is entered into a MySQL table.
include 'connect.php';
$table = "".$prefix."table";
$handle = fopen('php://memory', 'w+');
fwrite($handle, iconv('CP1251', 'UTF-8', file_get_contents('in.csv')));
rewind($handle);
$r = 0;
set_time_limit(0);
while (($row = fgetcsv($handle, 1000, ';', '"')) != FALSE)
{
$r++;
if($r == 1) {continue;} // Не дает записать в БД первую строку
$ins="INSERT INTO $table (`1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`) VALUES ('$row[0]', '$row[1]', '$row[2]', '$row[3]', '$row[4]', '$row[5]', '$row[6]', '$row[9]')";
mysql_query($ins);
if (mysql_errno()) {echo mysql_errno() . '\n' . mysql_error() . '\n';}
}
$ins=sprintf("INSERT INTO $table (`1`, `2`, `3`, `4`, `5`, `6`, `7`) VALUES ('$row[0]', '$row[1]', '$row[2]', '$row[3]', '%s', '$row[5]', '$row[6]')",mysql_real_escape_string($row[4]));
Answer the question
In order to leave comments, you need to log in
I may be wrong, but it seems to me that PHP takes this as an escape character. Try specifying an escape before the \ , perhaps there are other ways to indicate that this is not an escape character, but character text. Mb specify in quotes.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question