title: php PDO insert batch multiple rows with placeholders
body: <p>I am looking to do multiple inserts using PHP PDO.</p>
  
  <p>The closest answer I have found is this one</p>
  
  <p><strong><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</a></strong></p>
  
  <p>However the example thats been given uses ?? instead of real placeholders.</p>
  
  <p>I have looked at the examples on the PHP doc site for place holders</p>
  
  <p><strong><a href="http://php.net/manual/en/pdo.prepared-statements.php" rel="noreferrer">php.net pdo.prepared-statements</a></strong></p>
  
  <pre><code>$stmt = $dbh-&gt;prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
  $stmt-&gt;bindParam(':name', $name);
  $stmt-&gt;bindParam(':value', $value);
  </code></pre>
  
  <p>Now lets say I wanted to achieve the above but with an array</p>
  
  <pre><code>$valuesToInsert = array(
    0 =&gt; array('name' =&gt; 'Robert', 'value' =&gt; 'some value'),
    1 =&gt; array('name' -&gt; 'Louise', 'value' =&gt; 'another value')
  );
  </code></pre>
  
  <p><strong>How would I go about it with PDO and multiple inserts per transaction?</strong></p>
  
  <p>I imagine it would start of with a loop?</p>
  
  <pre><code>$stmt = $dbh-&gt;prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
  
  foreach($valuesToInsert as $insertRow){
  
      // now loop through each inner array to match binded values
      foreach($insertRow as $column =&gt; value){
          $stmt-&gt;bindParam(":{$column}", value);
      }
  }
  $stmt-&gt;execute();
  </code></pre>
  
  <p>However the above does not work but hopefully will demonstrate what im trying to achieve</p>