title: Why is this prepared PHP PDO statement not working when the query() is?
body: <p>I'm probably being stupid, but I have this function, which calculates the number of pages necessary based on the input, from which it counts how many pages are necessary and returns it.</p>
  
  <pre><code>function get_total_pages($field, $table, $page_size = 20, $where_something = "", $equals_something = ""){
      global $dbh;
      try {
          if(empty($where_something)){
          // I deleted irrelevant code here
          }
          elseif(!empty($where_something) &amp;&amp; !empty($equals_something)){
              $count_query = $dbh-&gt;prepare("SELECT COUNT(:field) FROM :table WHERE :where=:equals");
              $count_query-&gt;bindParam(":field", $field);
              $count_query-&gt;bindParam(":table", $table);
              $count_query-&gt;bindParam(":where", $where_something);
              $count_query-&gt;bindParam(":equals", $equals_something);
              $count_query-&gt;execute();
              $count = $count_query-&gt;fetch();
              $total_records = $count[0];                 // calculating number of records in history table
              $total_pages = ceil($total_records / $page_size);   // calculating number of pages necessary
              return $total_pages;
          }
          return false;
      }
      catch(PDOException $e){
          echo $e-&gt;getMessage();
      }
  </code></pre>
  
  <p>I call it with<br>
  <code>$total_pages = get_total_pages("username", "comments", $page_size, "username", $_GET['user']);</code></p>
  
  <p>Here is the error I get:</p>
  
  <p><code>SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''comments' WHERE 'username'='redbot'' at line 1</code></p>
  
  <p>However, if I swap all the function's code for a simpler query() instead of a prepared statement, it works, as long as I append quotation marks to the username:</p>
  
  <pre><code>function get_total_pages($field, $table, $page_size = 20, $where_something = "", $equals_something = ""){
      global $dbh;
      try {
          if(empty){
            //   irrelevant code
          }
          elseif(!empty($where_something) &amp;&amp; !empty($equals_something)){
              $count_query = $dbh-&gt;query("SELECT COUNT({$field}) FROM {$table} WHERE {$where_something}={$equals_something}");
              $count = $count_query-&gt;fetch();
              $total_records = $count[0];                 // calculating number of records in history table
              $total_pages = ceil($total_records / $page_size);   // calculating number of pages necessary
              return $total_pages;
          }
          return false;
      }
      catch(PDOException $e){
          echo $e-&gt;getMessage();
      }
  }
  </code></pre>
  
  <p><code>$total_pages = get_total_pages("username", "comments", $page_size, "username", "\"" . $_GET['user'] . "\"");</code></p>