I need to use a full text search to get the rows where the column contains the word O'henry
My code:
$word = "O'henry";
$sql = "SELECT * FROM mytable WHERE to_tsvector('english', col) @@ to_tsquery(:word) ";
$sth = $db->prepare( $sql );
$sth->execute(
array(":word"=>$word)
);
I have two problems:
1) This query matches columns which contains the word O'henry
, but it also matches columns containing, for example: "O yes, thierry henry is good sportsman."
2) If $word
begins with quotes, for example 'henry
, I got an error: syntax error in tsquery: "'henry"
, in spite of the fact that the search string was already escaped.
How can I solve this problem?