1

I'm having an issue trying to bind and execute a PDO statement using PHP and a REGEXP for a SQLite database. I have a column containing string words separated by spaces, and I am using the following REGEXP (?<!\S)WORD(?!\S) to match complete words that are surrounded by white space. The following SQL query works when I run in SQLite studio and gives me the desired results:

SELECT * FROM Questions WHERE Tags REGEXP '(?<!\S)WORD(?!\S)'

However, when I try to use it in a PDO statement, it keeps failing. So far I have tried:

$sql = "SELECT * FROM Questions WHERE Tags REGEXP :tags";
$tags = '(?<!\S)WORD(?!\S)';
$statement = $db -> prepare($sql);
$statement -> bindParam(':tags', $tags);

and I have also tried escaping backslashes as well as running the query directly without using any prepared statements and simply using a known working query and it still seems to fail. Ex w/o binds:

$sql = "SELECT * FROM Questions WHERE Tags REGEXP '(?<!\S)TAG(?!\S)'";
$statement = $db -> query($sql);

However that doesn't seem to work with the REGEXP either. If I change it to a simple SELECT * from the entire table or any non-REGEXP query everything seems to work fine until I add in those REGEXPs. I'm pretty new to PHP so I'm not entirely sure what is going on here or where to look. Any help would be appreciated!

Eric D
  • 11
  • 1
  • 1
    While the `REGEXP` operator _does_ exist for SQLite3, you need to provide an implementation for it to use, q.v. [here](http://stackoverflow.com/questions/5071601/how-do-i-use-regex-in-a-sqlite-query). – Tim Biegeleisen Mar 27 '17 at 01:04
  • 1
    I will definitely have to read up more about SQLite, but for now that link you sent provided some hacky solutions that I was able to get to work without using the REGEXP at all...thanks! – Eric D Mar 27 '17 at 01:20
  • Yes, you can just use `LIKE`, but it would be really ugly, so I didn't post an answer. – Tim Biegeleisen Mar 27 '17 at 01:22
  • Yeah, its definitely not pretty...but it will do for now until I can figure out how to implement those REGEXPs! – Eric D Mar 27 '17 at 01:24

0 Answers0