我正在尝试搜索关键字
的字段名字和姓氏$q1 = strtolower($_GET["q"]);
$q=str_replace(" ","%",$q1);
$sql = "select DISTINCT users.*, user_id FROM users WHERE $email_filter
firstname LIKE '%$q%' OR lastname LIKE '%$q%' ORDER BY lastname";
$rsd = mysql_query($sql);
while($rs = mysql_fetch_array($rsd)) { echo $results }
这就是我到目前为止所遇到的问题,问题是如果你用John Doe作为一个例子,一旦你输入John就会找到它,它会找到它,但是john doe ...没有结果
答案 0 :(得分:0)
像
这样的东西SELECT * FROM users where CONCAT(firstname, ' ', lastname) like '%$q%'
或
SELECT * FROM users where CONCAT_WS(' ', firstname, lastname) like '%$q%'
如果需要倒车,请尝试以下方法:
SELECT * FROM users where CONCAT_WS(' ', firstname, lastname) like '%$q%'
or CONCAT_WS(' ', lastname, firstname) like '%$q%'
(即,如果搜索“A B”应该返回“A B”以及“B A”)
答案 1 :(得分:0)
我建议您绑定变量。否则你会接受sql注射。
$stmt = $mysqli->prepare("select * from users where firstname like ? AND lastname like ?");
$stmt->bind_param('ss', $firstname,$lastname);
答案 2 :(得分:0)
您必须拆分查询字符串并搜索每个字词
$query_terms = explode(" ", $q1);
$conditions = ''
foreach($query_terms as $term){
$conditions = $conditions.' firstname LIKE "%'.$term.'%" OR lastname LIKE "%'.$term.'%"';
}
$sql = "select DISTINCT users.*, user_id FROM users WHERE $email_filter $conditions ORDER BY lastname";