要在 PHP 中搜索包含空格的关键词,并使用 MySQL 语句进行查询,你可以使用 MySQL 的 LIKE 运算符和 % 通配符来实现。
MySql拼接语句
$condition = '';
if (isset($word) && $word != '')
{
$keyworksqland = '';
$wordlist = str_replace("+", " ", $word);
$wordlist = str_replace(" ", " ", $wordlist);
$wordlistx = explode(" ", $wordlist);
$condition .= " and ( ";
for ($k = 0; $k < count($wordlistx); $k++) {
if ($k > 0) {
$keyworksqland = " and ";
}
$condition .= $keyworksqland . " name like '%" . addslashes($wordlistx[$k]) . "%'";
}
$condition .= " )";
}
mysql运行结果
and ( name like ‘%源代码%’ and name like ‘%数据%’ and name like ‘%UI%’ )
@漏刻有时