title: Bulk Parameterized Inserts
body: <p>I'm trying to switch some hard-coded queries to use parameterized inputs, but I've run into a problem:  How do you format the input for parameterized bulk inserts?</p>
  
  <p>Currently, the code looks like this:</p>
  
  <pre><code>$data_insert = "INSERT INTO my_table (field1, field2, field3) ";
  $multiple_inserts = false;
  while ($my_condition)
  {
      if ($multiple_inserts)
      {
          $data_insert .= " UNION ALL ";
      }
  
      $data_insert .= " SELECT myvalue1, myvalue2, myvalue3 ";
  }
  
  $recordset = sqlsrv_query($my_connection, $data_insert);
  </code></pre>
  
  <p>A potential solution (modified from <a href="https://stackoverflow.com/questions/4629022/how-to-insert-an-array-into-a-single-mysql-prepared-statement-w-php-and-pdo">How to insert an array into a single MySQL Prepared statement w/ PHP and PDO</a>) appears to be:</p>
  
  <pre><code>$sql = 'INSERT INTO my_table (field1, field2, field3) VALUES ';
  $parameters = array();
  $data = array();
  while ($my_condition)
  {
      $parameters[] = '(?, ?, ?)';
      $data[] = value1;
      $data[] = value2;
      $data[] = value3;
  }
  
  if (!empty($parameters)) 
  {
      $sql .= implode(', ', $parameters);
      $stmt = sqlsrv_prepare($my_connection, $sql, $data);
      sqlsrv_execute($stmt);
  }
  </code></pre>
  
  <p>Is there a better way to accomplish a bulk insert with parameterized queries?</p>