我在Sphinx论坛上找到了几个讨论这个问题的主题:
http://sphinxsearch.com/forum/view.html?id=5974
我创建数据库连接的功能:
function createSphinxPdo()
{
try {
$options = [
\PDO::ATTR_EMULATE_PREPARES => true,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
];
$db = new \PDO("mysql:host=127.0.0.1;port=3308;charset=utf8mb4", null, null, $options);
} catch (\PDOException $e) {
throw $e;
}
return $db;
}
这很好用:
$sphinxDb = createSphinxPdo();
$stmt = $sphinxDb->prepare('
SELECT *, WEIGHT() AS w FROM my_index
WHERE MATCH(\'@column "word"/1\')
');
try {
$success = $stmt->execute();
} catch (\PDOException $e) {
throw $e;
}
$results = $stmt->fetchAll(\PDO::FETCH_ASSOC);
echo '$results<pre>' . PHP_EOL;
print_r($results);
echo PHP_EOL . '</pre>';
未返回任何结果:
$stmt = $sphinxDb->prepare('
SELECT *, WEIGHT() AS w FROM my_index
WHERE MATCH(\'@column ":word"/1\')
');
try {
$success = $stmt->execute([':word' => 'word']);
} catch (\PDOException $e) {
throw $e;
}
$results = $stmt->fetchAll(\PDO::FETCH_ASSOC);
echo '$results<pre>' . PHP_EOL;
print_r($results);
echo PHP_EOL . '</pre>';
当我将值绑定到预准备语句时,不会返回任何结果。
答案 0 :(得分:1)
你可以这样做:
$stmt = $sphinxDb->prepare('
SELECT *, WEIGHT() AS w FROM my_index
WHERE MATCH(:query)
');
....
$success = $stmt->execute([':query' => '@column "word"/1']);
即参数应该是整个扩展查询。
为了配合您的其他问题,您可以直接对数组的值使用EscapeString,例如
$success = $stmt->execute([':query' => EscapeString($query)]);
如果不想允许任何扩展查询语法。 EscapeString将负责查询语法,pdo / mysql-client将负责SphixnQL的引用。