当您的搜索词中有空格时,您通常需要将字符串拆分为多个部分,然后再将其放入查询中。如果AND
您想匹配所有输入的搜索词,或者OR
如果您想匹配任何一个,请使用。例子:
Search: john james smith
<?php
$search = "john james smith";
$bits = explode(" ", $search);
foreach($bits as $key => $bit) {
$bits[$key] = '(`field1` LIKE "' . $bit . '" AND `field2` LIKE "' . $bit . '")';
}
$sql = implode(' OR ', $bits);
// (`field1` LIKE "john" AND `field2` LIKE "john") OR (`field1` LIKE "james" AND `field2` LIKE "james") OR (`field1` LIKE "smith" AND `field2` LIKE "smith")
?>
演示:https ://eval.in/66808
看起来您正在使用 PDO(这很好),因此您只需将绑定参数放入该字符串,然后在当前 SQL 查询的末尾将其连接到WHERE
子句中。