title: PDO MySQL: Insert multiple rows in one query
body: <p>Hello I am making a class for doing multiple insert in pdo.</p>
  
  <p>It is something like this</p>
  
  <pre><code>INSERT INTO $table (key1,key2,key3,etc) VALUE (value1,value2,value3,etc), (value1,value2,value3,etc), (value1,value2,value3,etc)
  </code></pre>
  
  <p>After searching I found out that I have to build something like</p>
  
  <pre><code>INSERT INTO $table (key1,key2,key3,etc) VALUE (:key1,:key2,:key3,etc), (:key1,:key2,:key3,etc), (:key1,:key2,:key3,etc)
  </code></pre>
  
  <p>then execute with this <code>$this-&gt;execute($data);</code>
  where <code>$data</code> is</p>
  
  <pre><code> 0 =&gt; 
      array
        'key1' =&gt; 'value1'
        'key2' =&gt; 'value2'
        'key3' =&gt; 'value3'
   1 =&gt; 
      array
        'key1' =&gt; 'value1'
        'key2' =&gt; 'value2'
        'key3' =&gt; 'value3'
  
   etc
  </code></pre>
  
  <p><strong>the problem is i still get an error <code>Array to string conversion</code> on <code>$insert-&gt;execute($data);</code> how can i fix that?</strong></p>
  
  <p>here's a snippet of what I'm making.</p>
  
  <pre><code>public function multipleInsert($table, $data = array()) 
  {
  
      # INSERT (name) VALUE (value),(value)
      if (count($data) &gt; 1) 
      {
          $fieldnames = array_keys($data[0]);
          $count_inserts = count(array_values($data));
          $count_values = count(array_values($data[0]));
  
          # array(????) untill x from first data
          for($i = 0; $i &lt; $count_values; $i++)
          {
              $placeholder[] = '?';
          }
  
          # array((????),(????),(????)) for query
          for ($i=0; $i &lt; $count_inserts; $i++) 
          { 
              $placeholders[] = '('. implode(',',$placeholder) . ')';
          }
  
          $query  = 'INSERT INTO '. $table;
          $query .= '(`'. implode('`, `', $fieldnames) .'`)';
          $query .= ' VALUES '. implode(', ', $placeholders);
  
          $insert = $this-&gt;start-&gt;prepare($query);
  
          $i = 1;
          foreach($data as $item) 
          {
              foreach ($item as $key =&gt; $value) 
              {
                 $insert-&gt;bindParam($i++, $item[$key]);
              }
          }
  
          echo $query;
          $insert-&gt;execute();
  
          $return['status'] = true;
          $return['lastid'] = $this-&gt;start-&gt;lastInsertId();
  
          return $return;
      } 
      else 
      {
          die('$data is less then two array, use single insert instead.');
      }
  }
  </code></pre>