title: PDO Dynamic Query Building
body: <p>I have been old school using mysql_query and starting out now using PDO. Which is great!</p>
  
  <p>But in my old scripts I had build a dynamic query builder, and i'm having a tough time porting that over using PDO.</p>
  
  <p>If anyone can give me some direction that would be great!</p>
  
  <p>Here is the theory of it. </p>
  
  <ol>
  <li>I have an array of </li>
  <li>the DB Fields and Values (upon insert).</li>
  <li>Create the query string to product a valid PDO transaction</li>
  </ol>
  
  <p>Here is a portion of what i'm trying to do.</p>
  
  <pre><code>public $dbFields; // This is an array of the fields plus VALUES
  
  public function select($where, $limit) {
      // This is what I **had** before
      $query = "SELECT ". implode(", ", $this-&gt;dbFields) ." FROM ". $this-&gt;table." WHERE ". $where ." ". $limit."";
  
      // Now i need to convert that to PDO
      $this-&gt;connection-&gt;beginTransaction();
  
      # START Query
      $select = $this-&gt;connection-&gt;prepare("SELECT {$this-&gt;fieldNames} FROM {$this-&gt;table}");
  
      // I need to BIND my params and values, but i'm not sure the best route to take when I have a WHERE clause that includes, "AND" / "OR" operators.
  
      # EXECUTE the query
      $select-&gt;execute();
  
      $this-&gt;connection-&gt;commit();
  }
  </code></pre>
  
  <p>This is what I <strong>HAD</strong> before</p>
  
  <pre><code>$results = $db-&gt;select("userId = 111 OR userId = 222");
  </code></pre>
  
  <p>But what i'm <strong>thinking</strong> I need to do is use something more like</p>
  
  <pre><code>$results = $db-&gt;select(array("userId"=&gt;111, "userId"=&gt;222));
  </code></pre>
  
  <p>I know this is a tall order, and I hope it makes sense in what i'm trying to do, but any help in trying to build these queries would be greatly appreciated. </p>