title: PDO Prepared Statement - Different behavior inside 2 similar methods
body: <p>I would like someone to explain me why the first method works only after quoting the placeholder <code>':cat_id'</code> in the <code>WHERE</code> clause, and requires the <code>$this-&gt;db-&gt;query($query);</code>, otherwise it throws fatal error: </p>
  
  <blockquote>
    <p>"SQLSTATE[HY093]: Invalid parameter number: number of bound variables
    does not match number of tokens in..."</p>
  </blockquote>
  
  <p>while the second method doesn't need neither quoting and the <code>$this-&gt;db-&gt;query()</code> method?</p>
  
  <pre><code>public function getAllPosts($cat_id = null)
  {   
      // Query build
      $query = "SELECT posts.*, users.username, categories.title FROM posts "
              . "INNER JOIN users "
              . "ON posts.user_id = users.id "
              . "INNER JOIN categories "
              . "ON posts.category_id = categories.id ";
      // Filter
      if (!is_null($cat_id))
      {
          $query .= "WHERE category_id = ':cat_id' ";
  
          // Order query
          $query .= "ORDER BY create_date DESC";
          $this-&gt;db-&gt;bind(':cat_id', $cat_id);
      }
      else
      {
          $query .= "ORDER BY create_date DESC";
  
      }
      $this-&gt;db-&gt;query($query);
      // Run the query
  
  
      // Assign Result Set
      $results = $this-&gt;db-&gt;resultset();
      return $results;
  }
  
  
  public function getCategoryPosts($cat_id)
  {   
      $query = "SELECT posts.*, users.username, categories.title FROM posts "
              . "INNER JOIN users "
              . "ON posts.user_id = users.id "
              . "INNER JOIN categories "
              . "ON posts.category_id = categories.id "
              . "WHERE posts.category_id = :cat_id "
              . "ORDER BY create_date DESC";
  
      $this-&gt;db-&gt;bind(':cat_id', $cat_id);
  
      $results = $this-&gt;db-&gt;resultset();
      return $results;
  }
  </code></pre>
  
  <h2>Update:</h2>
  
  <p>Here are a echo output of the query from the first method:</p>
  
  <h3>1. with quoted:</h3>
  
  <pre><code>SELECT posts.*, users.username, categories.title FROM posts 
  INNER JOIN users ON posts.user_id = users.id
  INNER JOIN categories ON posts.category_id = categories.id 
  WHERE category_id = ':cat_id' ORDER BY create_date DESC
  </code></pre>
  
  <h3>2. unquoted:</h3>
  
  <pre><code>SELECT posts.*, users.username, categories.title FROM posts 
  INNER JOIN users ON posts.user_id = users.id 
  INNER JOIN categories ON posts.category_id = categories.id 
  WHERE category_id = :cat_id ORDER BY create_date DESC
  </code></pre>