title: Use bound parameter multiple times
body: <p>I'm trying to implement a pretty basic search engine for my database where the user may include different kinds of information. The search itself consists of a couple of a union selects where the results are always merged into 3 columns.</p>
  
  <p>The returning data however is being fetched from different tables.</p>
  
  <p>Each query uses $term for matchmaking, and I've bound it to ":term" as a prepared parameter.</p>
  
  <p>Now, the manual says:</p>
  
  <blockquote>
    <p>You must include a unique parameter marker for each value you wish to pass in to the statement when you call PDOStatement::execute(). You cannot use a named parameter marker of the same name twice in a prepared statement.</p>
  </blockquote>
  
  <p>I figured that instead of replacing each :term parameter with :termX (x for term = n++) there must be a be a better solution?</p>
  
  <p>Or do I just have to bind X number of :termX?</p>
  
  <p><strong>Edit</strong> Posting my solution to this:</p>
  
  <pre><code>$query = "SELECT ... FROM table WHERE name LIKE :term OR number LIKE :term";
  
  $term = "hello world";
  $termX = 0;
  $query = preg_replace_callback("/\:term/", function ($matches) use (&amp;$termX) { $termX++; return $matches[0] . ($termX - 1); }, $query);
  
  $pdo-&gt;prepare($query);
  
  for ($i = 0; $i &lt; $termX; $i++)
      $pdo-&gt;bindValue(":term$i", "%$term%", PDO::PARAM_STR);
  </code></pre>
  
  <p>Alright, here is a sample. I don't have time for sqlfiddle but I will add one later if it is necessary.</p>
  
  <pre><code>(
      SELECT
          t1.`name` AS resultText
      FROM table1 AS t1
      WHERE
          t1.parent = :userID
          AND
          (
              t1.`name` LIKE :term
              OR
              t1.`number` LIKE :term
              AND
              t1.`status` = :flagStatus
          )
  )
  UNION
  (
      SELECT
          t2.`name` AS resultText
      FROM table2 AS t2
      WHERE
          t2.parent = :userParentID
          AND
          (
              t2.`name` LIKE :term
              OR
              t2.`ticket` LIKE :term
              AND
              t1.`state` = :flagTicket
          )
  )
  </code></pre>