MySQL PDO: Using bound placeholder within a regular expression
<p>Here's a PHP snippet attempting to fetch some rows from a MySQL table:</p>

<pre><code>$connect = dbconn(PROJHOST,'dbcontext', PROJDBUSER, PROJDBPWD);
$sql= "SELECT contextleft, contextright 
       FROM tblcontext 
       WHERE contextleft REGEXP '[[:&lt;:]]:word[[:&gt;:]]' 
       LIMIT 0, 10";
$xleft = array();
$xright = array();
$countrows = 0;
$query = $connect-&gt;prepare($sql);
$query-&gt;bindParam(':word', $term);
if($query-&gt;execute()) {
    $rows = $query-&gt;fetchAll(PDO::FETCH_ASSOC);
    foreach($rows as $row){
        $xleft[$countrows] = $row['contextleft'];
        $xright[$countrows] = $row['contextright'];
        $countrows++;
    }
    $notfound = null;
}
</code></pre>

<p>I reckon there's some problem with the following line in the above code:</p>

<pre><code>$sql= "SELECT contextleft, contextright FROM tblcontext WHERE contextleft REGEXP '[[:&lt;:]]:word[[:&gt;:]]' LIMIT 0, 10";
</code></pre>

<p>Basically, I need to run a regex that matches a word (bound by the <em>:word</em> placeholder) and returns matching records. The code is otherwise fine because it works without the regex. But somehow it refuses to return anything when I use the regex. Is there something wrong with the way I have used the placeholder parameter within the regex?</p>

<p>In a nutshell, my question is how can one use a bound placeholder inside of a regular expression?</p>

<p><strong>ADDITIONAL INFO</strong>: The table is named <strong>tblcontext</strong> with the following structure:</p>

<pre><code>Engine MyISAM
sn: int(11), auto-increment, primary key
contextleft: varchar(190), fulltext index, unique
contextright: varchar(190), fulltext index, unique
</code></pre>

<p>Some random examples that should match when the user enters, say, <em>fly</em>:</p>

<pre><code>I want to fly
To fly is easy
She will never fly because she is too heavy
</code></pre>

<p>Some random examples that should NOT match when the user enters <em>fly</em>:</p>

<pre><code>Flying is easy
There is a blowfly on your food
She has been flying since last night
</code></pre>

<p>I cannot use FULLTEXT search for examples like the above because it only works for search strings larger than 3 characters.</p>