title: Insert multiple values via PHP PDO into MySQL database
body: <p>I want to insert 20 values at once <strong>via PHP Data Objects</strong>. At the moment I'm doing it this way, it seems <em>very ugly</em>:</p>
  
  <pre><code>    $stmt = $this-&gt;pdo-&gt;prepare("INSERT INTO &lt;tablename&gt; (`uid`, `mid`, `status`) 
                                  VALUES  (:uid, :mid1, :status), (:uid, :mid2, :status), 
                                          (:uid, :mid3, :status), (:uid, :mid4, :status),
                                          (:uid, :mid5, :status), ... and so on ...");
      $stmt-&gt;bindParam(':uid', $this-&gt;uid);
  
      $random_mid = array_rand(range(1,20), 5);
  
      $ref = new Mooney($random_mid[0]);
      $myMid1 = $ref-&gt;getMid();
      $stmt-&gt;bindParam(':mid1', $myMid1);
  
      $ref = new Mooney($random_mid[1]);
      $myMid2 = $ref-&gt;getMid();
      $stmt-&gt;bindParam(':mid2', $myMid2);
  
      $ref = new Mooney($random_mid[2]);
      $myMid3 = $ref-&gt;getMid();
      $stmt-&gt;bindParam(':mid3', $myMid3);
  
      ... and so on ...
  
      $status = 'open'; // default starting value of status
      $stmt-&gt;bindParam(':status', $status);
      $stmt-&gt;execute();
  </code></pre>
  
  <p>Is there a better solution by using a for-loop or so? Hope you can provide a better solution / more beautiful solution. Thanks for your time and help!</p>
  
  <p><strong>EDIT 1</strong>: Now thanks to <strong>@Jurik</strong> the code works and look like this:</p>
  
  <pre><code>private function insertValuesAtOnce() {
      $stmt = $this-&gt;pdo-&gt;prepare('INSERT INTO `&lt;tablename&gt;` (`uid`, `mid`, `status`) VALUES  (:UID, :MID, :STATUS)');
      $random_mid = $this-&gt;UniqueRandomNumbersWithinRange(1, 20, 7);
      require_once('&lt;some path&gt;');
      $status = 'open';
      foreach($random_mid AS $val){
          $ref = new Mooney($val);
          $myMid = $ref-&gt;getMid();
  
          $stmt-&gt;execute(array(
              ':UID' =&gt; $this-&gt;uid,
              ':MID' =&gt; $myMid,
              ':STATUS' =&gt; $status
          ));
      }
  }
  </code></pre>
  
  <p>But it seems to me, that this is not a "insert many rows at once" but more "insert a single row multiple times"</p>
  
  <p><strong>EDIT 2:</strong> Because it does not seem to be possible to insert all values at once, I will use the solution to insert them row by row, but wrapping it into a transaction! This improved the runtime from  0,3915548324585 sec/serialize to  0,074591159820557 sec/serialize.</p>
  
  <pre><code>private function insertValuesAtOnce() {
      $stmt = $this-&gt;pdo-&gt;prepare('INSERT INTO `&lt;tablename&gt;` (`uid`, `mid`, `status`) VALUES  (:UID, :MID, :STATUS)');
      $random_mid = $this-&gt;UniqueRandomNumbersWithinRange(1, 20, 7);
      require_once('&lt;some path&gt;');
      $status = 'open';
  
      // Beginn Transaction (ACID)     
      $this-&gt;pdo-&gt;beginTransaction();
  
      foreach($random_mid AS $val){
          $ref = new Mooney($val);
          $myMid = $ref-&gt;getMid();
  
          $stmt-&gt;execute(array(
              ':UID' =&gt; $this-&gt;uid,
              ':MID' =&gt; $myMid,
              ':STATUS' =&gt; $status
          ));
      }
      // End Transaction (ACID)
      $this-&gt;pdo-&gt;commit();
  }
  </code></pre>