php - How to escape asterisk and question mark using RLIKE in PDO? -
i have query search posts content. i'm still using mysql 5.5 innodb rlike seems 1 of choices:
$sql = "select title,content table1 content rlike ?"; $i = 1; $users = $dbh->prepare($sql); $users->bindvalue($i++, $purifier->purify($_get['content']), pdo::param_str); $users->execute(); but found when enter asterisk or question mark parameter value (e.g www.site.com?content=*), i'm getting
sqlstate[42000]: syntax error or access violation: 1139 got error 'repetition-operator operand invalid' regexp. how can avoid error? * , ? 2 special characters cause error?
rlike requires valid regular expression operand. means 'yes, there other characters * , ? cause error.'
how avoid? depends on want achieve exactly. easiest avoid regular expressions altogether. if need user can enter regular expression, best way validate use in query , catch error if malformed.
if want find content contains string, using like more reliable. , need escape % , _.
$search = '%'.str_replace(array('%', '_'), array('\%', '\_'), $_get['content']).'%'; $sql = "select title,content table1 content ?"; ...
Comments
Post a Comment