gpt4 book ai didi

PHP 搜索使用 == 且不区分大小写

转载 作者:行者123 更新时间:2023-11-29 02:45:56 25 4
gpt4 key购买 nike

我有一个 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;
}
}
?>

最佳答案

使用 MySql LOWER执行不区分大小写搜索的函数:

...

foreach ($keywords as $key => $keyword) {
$keyword = strtolower($keyword);
$where .= " LOWER(`keywords`) LIKE '%$keyword%'";
if ($key != ($total_keywords -1)) {
$where .= " AND ";
}
}

关于PHP 搜索使用 == 且不区分大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42186388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com