Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
466 views
in Technique[技术] by (71.8m points)

SQLite FTS5 through PHP/PDO - How to bind values while filtering on a column name?

In a FTS5 MATCH clause, column names used as filters are declared with an ending colon, like in:

... WHERE ftstable MATCH 'colname: keyword'

(as per https://sqlite.org/fts5.html#fts5_column_filters)

When I try to declare 'keyword' as a bound value, like in:

$sql = "SELECT * FROM ftstable WHERE ftstable MATCH 'colname: :keyword'";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(':keyword' => 'keyword'));

I get the following error:

SQLSTATE[HY000]: General error: 1 unrecognized token: ":"

because of the colon following the column name.

I get the same error using alternate syntax (? placeholders, bindValue(), etc.).

Does anyone know of a workaround here? Or am I missing something obvious? Many thanks.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can't have parameters in string literals; there's no interpolation done looking for them. You can, however, build an argument to MATCH using string concatenation:

$sql = "SELECT * FROM ftstable WHERE ftstable MATCH ('colname: ' || :keyword)";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...