A
A
Angelxalfa2015-02-27 11:48:17
PHP
Angelxalfa, 2015-02-27 11:48:17

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';}
   }

After entering the data into the table, the \ symbols disappear (while the / symbols remain intact).
How to deal with these? Thanks in advance!
Solved the problem thanks to ILoveYAnny:
change the Msql query to
$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]));

(in my case the problem was in $row[4])

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ILoveYAnny, 2015-02-27
@Angelxalfa

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 question

Ask a Question

731 491 924 answers to any question