我有一个PHP代码,我想使用==运算符只搜索确切的术语,并且搜索不区分大小写。当我将LIKE改为==时,第一件事不起作用。
<?php
$con = mysql_connect('********','********','********');
mysql_select_db("********",$con);
function search_results($keywords){
$returned_results = array();
$where ="";
$keywords = preg_split('/[\s]+/',$keywords);
$total_keywords = count($keywords);
foreach($keywords as $key=>$keyword){
$where .= "`keywords` LIKE '%$keyword%'";
if($key != ($total_keywords -1)){
$where .=" AND ";
}
}
$results = "SELECT name, image_url, game_url, alt FROM search_games WHERE $where";
$results_num = ($results =mysql_query($results))? mysql_num_rows($results):0;
if($results_num === 0){
return false;
}
else{
while ($results_row = mysql_fetch_assoc($results)) {
$returned_results[] = array(
'name' => $results_row['name'],
'image_url' => $results_row['image_url'],
'game_url' => $results_row['game_url'],
'alt' => $results_row['alt']
);
}
return $returned_results;
}
}
?>
答案 0 :(得分:1)
使用MySql LOWER函数执行不敏感搜索:
{{1}}
答案 1 :(得分:0)
您可以使用以下代码替换foreach:
foreach($keywords as $key=>$keyword){
$keyword = strtoupper($keyword);
$where .= "upper(`keywords`) = '$keyword'";
if($key != ($total_keywords -1)){
$where .=" AND ";
}
}
上面的代码使$keyword
字符串为大写,然后在sql查询中将其与upper(keywords)
进行比较,因此在比较时它们都是大写。
来源: