title: php pdo function connect to DB and do query
body: <p>I am trying to be as efficient with code as possible. Unfortunately, I can't seem to figure out a good way of doing this. I have tried using Sammitch's code which does look cleaner but unfortunately it doesn't seem to work.</p>
  
  <p>I would like a way to stop having to use prepare, execute, every time and a function to me makes the most sense. Using Simmitch's suggestion, I removed the initial connection to database to stop unnecessary overheads but the code still does not work. Showing a "SQLSTATE[HY093]: Invalid parameter number: parameter was not defined" error.</p>
  
  <p>My code at present (some parts omitted as not necessary):</p>
  
  <pre><code>/*Function to talk to database*/
  function doQuery($myDB, $myQuery, $myValues)
  {
      try
      {     
          $st = $myDB-&gt;prepare($myQuery);       
          $st-&gt;execute($myValues);
          //echo $success;
      }
      catch (PDOException $e)
      {
          echo "Failed because: " . $e-&gt;getMessage();
      }
  }
  
      $db = new PDO('mysql:host=localhost;dbname='dbanme';charset=utf8', 'dbuser', 'dbpass');
      $db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //Set error mode
      $query = "INSERT INTO users(login,pass,email,county) VALUES(:username,:password,:email,:count)";
      $values = array('username' =&gt; $_POST['username'],
                       'password' =&gt; $_POST['password1'],
                       'email' =&gt; $_POST['email'],
                       'county' =&gt; $_POST['county']
                       );
      doQuery($db, $query, $values);
  </code></pre>