<p>Trying to do this sort of thing...</p>

<pre><code>WHERE username LIKE '%$str%'
</code></pre>

<p>...but using bound parameters to prepared statements in PDO. e.g.:</p>

<pre><code>$query = $db-&gt;prepare("select * from comments where comment like :search");
$query-&gt;bindParam(':search', $str);
$query-&gt;execute();
</code></pre>

<p>I've tried numerous permutations of single quotes and % signs and it's just getting cross with me.</p>

<p>I seem to remember wrestling with this at some point before but I can't find any references. Does anyone know how (if?) you can do this nicely in PDO with named parameters?</p>

[{'answer_id': 60782, 'author': 'Polsonby', 'author_id': 137, 'author_profile': 'https://Stackoverflow.com/users/137', 'pm_score': 5, 'selected': True, 'text': '<p>Ah. Found a comment on php.net that reminded me of the answer; you need to wildcard your value before the bindParam is evaluated, and not worry about quoting it. So for example this works fine:</p>\n\n<pre><code>$str = "%$str%";\n$query = $db-&gt;prepare("select * from comments where comment like :search");\n$query-&gt;bindParam(\':search\', $str);\n$query-&gt;execute();\n</code></pre>\n'}, {'answer_id': 15377644, 'author': 'Dominic P', 'author_id': 931860, 'author_profile': 'https://Stackoverflow.com/users/931860', 'pm_score': 2, 'selected': False, 'text': '<p>5 years later, in case anyone else stumbles upon this, there is an alternative method I\'ve discovered. The accepted solution wasn\'t really feasible for my situation, but this method seems to get the job done as well:</p>\n\n<pre><code>$query = $db-&gt;prepare("select * FROM table WHERE field LIKE CONCAT(\'%\',:search,\'%\')");\n$query-&gt;bindParam(\':search\', $str);\n$query-&gt;execute();\n</code></pre>\n\n<p>I\'m not sure if there will be a performance hit due to the overhead of calling the <code>CONCAT</code> function, but I wanted to pass this along as an option. Hopefully it will help someone.</p>\n'}]
['https://Stackoverflow.com/questions/60779', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/137/']