title: Mysql dynamic sql statement with PDO and parameters
body: <p>I want to create a dynamic sql statement and run it using PDO. My problem is that i have some parameters and i cannot think of a way to pass the parameters.
  Ex  : </p>
  
  <pre><code>$query = "Select * from tbl_task where 1=1";
  if (!empty($name)) $query .= " AND name = ?";
  if (!empty($status)) $query .= " AND status = ?"
  $db_stmt = new PDOStatement();
  $db_stmt = $this-&gt;db-&gt;prepare($query);
  $db_stmt-&gt;bindParam (1,$name); 
  $db_stmt-&gt;bindParam (2,$status);
  </code></pre>
  
  <p>My parameters does not get binded and i don't know how many parameters i have to bind, unless i write the same if statements but with bindParam instructions.</p>
  
  <p>I tryed with <code>mysql_real_escape_string</code> instead bindParam to PDO but for some reason my parameters are added empty.</p>
  
  <p>Any idea on how can i build a dynamic query and bind parameters to PDO ?</p>
  
  <p>Edit 1 : </p>
  
  <pre><code>$arr = array();
  if (!empty($name)){
      $query .= " AND `name` like :NAME";
      $arr['NAME'] = $name;
  }
  $db_stmt = new PDOStatement();
  $db_stmt = $this-&gt;db-&gt;prepare($query);
  $db_stmt-&gt;execute($arr);
  </code></pre>
  
  <p>How can i write a "like" statement ? I tried </p>
  
  <pre><code>$query .= " AND `name` like :NAME" . "%";
  </code></pre>
  
  <p>and is not working.</p>