title: Import CSV rows to SQL (INSERT or UPDATE)
body: <p>I have tried to program a PDO PHP file to be executed by a cron job. My problem is not the cron job, but the fact that my script doesn't seem to work..</p>
  
  <p>I found this script <pre>http://www.php.happycodings.com/Database_Related/code11.html</pre> and changed some content. For example I didn't need the mail to sent to me, and I only have one .CSV file. The thing is with this script, that I would like to check every row, to see if it already exists in the database. If so I would like to update the row in SQL if not I want to insert the row in SQL.</p>
  
  <p>It doesn't show any errors or anything, but somehow it doesn't seem to insert any content in SQL. Every time I execute the file it runs through the page and end up blank.. seems right to me, since I have told it to die if an error occurs. </p>
  
  <pre><code>&lt;?php 
  
  try{
      $conn = new PDO("mysql:host=localhost;dbname=data", 'user', 'pass');
  }
  catch(PDOException $pe)
  {
      die('Connection error, because: ' .$pe-&gt;getMessage());
  }
  
  $columnheadings = 1; 
  
  
  $filecontents = file ('../../_resources/_csv/letters.csv');
  
  for($i=$columnheadings; $i&lt;sizeof($filecontents); $i++) { 
  
      list($f1, $f2, $f3)=explode(";",$filecontents[$i]); 
  
      $sth = $conn-&gt;prepare("SELECT * FROM test WHERE 1 = '".$f1."'");
  
      if ($sth-&gt;execute() &amp;&amp; $row = $sth-&gt;fetch())
          {
              $update = $conn-&gt;execute("UPDATE test SET 1 = '$f1', 2 = '$f2', 3 = '$f3' WHERE id='".$row['id']."'");
  
  
          }
      else 
          {
              $insert = $conn-&gt;execute('INSERT INTO test VALUES ($filecontents[$i])');
  
  
          }
  
  } 
  
  
  ?&gt;
  </code></pre>
  
  <p>It should be easy to see what I am trying to do in the script, so please tell me what to do, if you have any idea or suggestions.</p>
  
  <pre><code>SQL Database
  
   ----------------
  | id | 1 | 2 | 3 |
  |----------------|
  |  1 | A | A | A |
  |----------------|
  |  2 | B | B | B |
   ----------------
  
  
  .CSV File
  
   -----------
  | 1 | 2 | 3 |
  |-----------|
  | A | A | A |
  |-----------|
  | B | B | B |
  |-----------|
  | C | C | C |
   -----------
  </code></pre>
  
  <p>This means that my script should be able to ignore the A and B already existing in the SQL database and then only insert C.</p>
  
  <p>This is an example of @njk's idea.. But it still doesn't seem to work.. Do I need a server plugin to accept incoming .CSV files?</p>
  
  <pre><code>&lt;?php 
  
  try{
      $conn = new PDO("mysql:host=localhost;dbname=data", 'user', 'pass');
  }
  catch(PDOException $pe)
  {
      die('Connection error, because: ' .$pe-&gt;getMessage());
  }
  
  
  $sth = $conn-&gt;execute("LOAD DATA 
                         INFILE '../../_resources/_csv/letters.csv'
                         [REPLACE] INTO TABLE test [FIELDS [TERMINATED BY ';']]
                         [LINES TERMINATED BY ' '] [IGNORE 1 LINES] (1, 2, 3)");
  
  
  ?&gt;
  </code></pre>