title: PDO and MySQL UPDATE in Foreach Loop
body: <p>I'm having this problem that has me completely stumped. Here's my execution pattern. The PDO calls are nested in foreach loops.</p>
  
  <pre><code>foreach(){
      foreach(){
  
      }
  }
  </code></pre>
  
  <p>I'm using PDO with MySQL, and when I execute UPDATE queries back-to-back, they interfere with each other within the loop. I know they work individually from commenting out one set, and executing the other. Here's the code template I'm dealing with:</p>
  
  <pre><code>$set_data1 = "UPDATE data_table
                SET data_status = 'PROCESSED' 
                WHERE data_id = :data_id1";
  
  $stmt = $db-&gt;prepare($set_data1);
  
  $stmt-&gt;bindParam(':data_id1', $data_array1['data_id'], PDO::PARAM_INT);
  
  $stmt-&gt;execute();
  
  $set_data2 = "UPDATE data_table
                SET data_status = 'PENDING'
                WHERE data_id = :data_id2";
  
  $stmt = $db-&gt;prepare($set_data2);
  
  $stmt-&gt;bindParam(':data_id2', $data_array2['data_id'], PDO::PARAM_INT);
  
  $stmt-&gt;execute();
  </code></pre>
  
  <p>For some reason, when executing both queries within the nested foreach loops, the data from $set_data1 is being cancelled out by $set_data2. I've tried closing the cursor with $stmt->closeCursor(); I've tried using a single statement to prepare, and just binding new parameters to the statement. I've tried setting the $stmt and $db instances to null, and then re-instantiating them to no avail. I've tried using CASE THEN and IF conditionals within the query... nothing. Any info on what the problem is would be wonderful. I don't know if PDO has an error with calling UPDATES on the same table within a loop, because I've never had this problem elsewhere. Thanks in advance!</p>