PHP按名称和姓氏或中间名搜索

时间:2014-11-07 23:36:11

标签: php mysql

我有这样的名字:

" John Martin Bud" " John Erik"

确定。想象一下有人搜索" john bud"或者"芽约翰"。

我想回归" JOHN MARTIN BUD"

但我做的方式同时归功于" JOHN MARTIN BUD" &安培; " JOHN ERIK"

我这样做:

$keywords = $_POST['campobusca'];
$keywords=explode(' ',$keywords); 
$string = implode("%' AND name LIKE '%", $keywords);
$sql = "SELECT * FROM names WHERE name LIKE '%$string'";
.
.
.

如何执行此搜索?

由于

1 个答案:

答案 0 :(得分:0)

你有一个小错字:

$keywords = "john doe";
$keywords=explode(' ',$keywords); 
$string = implode("%' AND name LIKE '%", $keywords);
echo "SELECT * FROM names WHERE name LIKE '%$string'";

将输出:

SELECT * FROM names WHERE name LIKE '%john%' AND name LIKE '%doe'

您需要查询中缺少的尾随通配符:

$sql = "SELECT * FROM names WHERE name LIKE '%$string%'";
                                                     ^