title: Building queries with PDO?
body: <p>I have some ancient code which I want to convert to PDO:</p>
  
  <pre><code>&lt;?php
      function build_query() {
          // db connection here
  
          $the_query = "";
  
          if ( empty( $_GET['c'] ) ) {
              $the_query = "select * from table1";
  
              if ( ( isset( $_GET['y'] ) ) &amp;&amp; ( isset( $_GET['m'] ) ) ) {
                  $the_query .= " where y = " . $_GET['y'] . " and m = " .  $_GET['m'];
              }
          } elseif ( ( $_GET['c'] == "1" ) || ( $_GET['c'] == "2" ) ) {
              $the_query = "select * from table1 where GGG = " . $_GET['c'];
  
              if ( ( isset( $_GET['y'] ) ) &amp;&amp; ( isset( $_GET['m'] ) ) ) {
                  $the_query .= " and y = " . $_GET['y'] . " and m = " .  $_GET['m'];
              }
          } else {
              $the_query = "select * from table1";
  
              if ( ( isset( $_GET['y'] ) ) &amp;&amp; ( isset( $_GET['m'] ) ) ) {
                  $the_query .= " where y = " . $_GET['y'] . " and m = " .  $_GET['m'];
              }
  
              $the_query .= " and c = " . $_GET['c'];
          }
  
          return // use the query to return results $the_data;
      }
  ?&gt;
  </code></pre>
  
  <p>I can't seem to figure out how to recode this using PDO.  I have made a start below, but can't seem to get any further:</p>
  
  <pre><code>&lt;?php
      function build_query() {
          $the_data = "";
  
          $DBH = new PDO( "mysql:host=server;dbname=database", "user", "pass" );
          $DBH -&gt; setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  
          $STH = $DBH -&gt; prepare( "build query here" );
  
          $STH -&gt; bindParam( ':c', $_GET['c'], PDO::PARAM_INT );
          $STH -&gt; bindParam( ':y', $_GET['y'], PDO::PARAM_INT );
          $STH -&gt; bindParam( ':m', $_GET['m'], PDO::PARAM_INT );
  
          $STH -&gt; execute();
  
          $ROWS = $STH -&gt; fetchAll();
  
              foreach($ROWS as $ROW) {
              $output .= $ROW["a"] . " - " . $ROW["b"] . " - " . $ROW["c"] . " - " . $ROW["d"] . "&lt;br /&gt;";
              }
  
          $DBH = null;
  
          return $output; 
      }       
  ?&gt;
  </code></pre>