title: Why am I getting error SQLSTATE[HY093]: Invalid parameter number: ? How can I fix it?
body: <p>Based on this question  <a href="https://stackoverflow.com/questions/9858073/how-to-insert-array-into-mysql-using-pdo-and-bindparam">How to insert array into mysql using PDO and bindParam?</a></p>
  
  <p>I'm trying to insert values of an array into mysql via PDO.
  I'm having a hard time of it, because I keep getting the following error.
  SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
  for this line <code>$stmt-&gt;execute();</code></p>
  
  <p>I'm guessing the problem has something to do with this line </p>
  
  <p><code>$stmt-&gt;bindParam(':val$count', $val,PDO::PARAM_STR);</code> Specifically 'val$count', but I'm not sure exactly what is going wrong.</p>
  
  <p><strong>QUESTION:</strong> What am I doing wrong? How can I fix this?</p>
  
  <p>Anyway here is the code I'm using along with the sample array.</p>
  
  <pre><code>$lastInsertValue=87;
  $qid[0][0]=1;
  $qid[0][1]=1;
  $qid[1][0]=2;
  $qid[1][1]="null";
  $qid[2][0]=3;
  $qid[2][1]=0;
  $array_count = count($qid);
  
  if (isset($lastInsertValue)) 
  { 
      try 
      {
              $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
              $stqid=array();
              $a=0;       
                  for ($i=0; $i&lt;$array_count; $i++)
                  {       
                      $stqid[$a]=$lastInsertValue;
                      $a++;
                      $stqid[$a]=$qid[$i][0];
                      $a++;
                      $stqid[$a]=$qid[$i][1];
                      $a++;
                  }
  
              $sql = "INSERT INTO qresults (instance, qid, result) VALUES ( :val0, :val1, :val2)";
              $count = 0;
              $stmt = $dbh-&gt;prepare($sql);    
                  foreach ($stqid as $val)
                  {
                      $stmt-&gt;bindParam(':val$count', $val,PDO::PARAM_STR);
                      $count++;           
                  }
              $stmt-&gt;execute();
              $dbh = null;
      }
      catch(PDOException $e)
      {
          echo $e-&gt;getMessage();
      }
  }   
  </code></pre>