title: Escaping a CSV file before LOAD INFILE to MySQL
body: <p>I am working on a script that routinely uploads a CSV file into a MySQL database. The issue is that one of the fields in the CSV file contains user generated text, which may include quotations and other characters which are unfriendly to MySQL. </p>
  
  <p>I have determined that the most efficient way to upload the CSV file is via MySQL's 'LOAD DATA INFILE' command. Here is the command as it appears in MyPHPAdmin when I upload the CSV: </p>
  
  <pre><code>LOAD DATA LOCAL INFILE '/home/myfolder/tmp/property_re_1.csv' REPLACE INTO TABLE `markers`
      FIELDS TERMINATED BY ';' ENCLOSED BY '"' ESCAPED BY '\\'
      LINES TERMINATED BY '\r\n' # 2 rows affected.
  </code></pre>
  
  <p>Simply loading the CSV with this command does not work, as the process terminates as soon as it runs into a '"' and the "ESCAPED BY '\'" doesn't appear to be serving its purpose. </p>
  
  <p>In this case, I think I may have to escape the quotations in the CSV file programmatically with PHP first. Then load the "Escaped" file into MySQL with the LOAD DATA INFILE command in SQL. </p>
  
  <p>I am sure this is a common problem that has a "best practice" solution. Essentially, my script needs to "clean up" a CSV file before loading it into a MYSQL table.</p>
  
  <p>Here is a link to the actual SQL table and CSV file I am working with to help wiser minds wrap their heads around this one: <a href="https://www.dropbox.com/sh/4iq10i51qlqyq8q/UjEQwvXKDA" rel="nofollow">https://www.dropbox.com/sh/4iq10i51qlqyq8q/UjEQwvXKDA</a></p>
  
  <p>Thanks for your help in advance. </p>