php实战案例记录(13)关键词包含空格的并且搜索条件

要在 PHP 中搜索包含空格的关键词,并使用 MySQL 语句进行查询,你可以使用 MySQL 的 LIKE 运算符和 % 通配符来实现。

php实战案例记录(13)关键词包含空格的并且搜索条件_第1张图片

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%’ )


@漏刻有时

你可能感兴趣的:(php,php,android,开发语言)