PHP/Mysql搜索-区分大小写 [英] PHP/Mysql Search - Case sensitive
问题描述
我使用以下PHP和MySql从表中获取行,
Im using the following PHP and MySql to fetch rows from a table,
$search_word=$_GET['search_word'];
$search_word_new=mysql_escape_string($search_word);
$search_word_fix=str_replace(" ","%",$search_word_new);
$sql=mysql_query("SELECT * FROM tweets WHERE content LIKE '%$search_word_fix%' ORDER BY votes DESC LIMIT 20");
内容"字段是包含推文的TEXT字段.
The 'content' field is a TEXT field containing tweets.
我遇到的问题是,如果我搜索" S tackoverflow",则得到的所有结果都包含"Stackoverflow",但是当文本为" s tackoverflow"时没有任何结果.基本上,搜索是区分大小写的.
The problem I have is if I search 'Stackoverflow' I get all the results containing 'Stackoverflow' but no results when the text is 'stackoverflow'. Basically the search is case sensitive.
是否可以更改查询或PHP,以便在搜索"Stackoverflow"时同时返回大写和小写结果?
Is it possible to change the query or PHP so when searching for 'Stackoverflow' both upper and lower case results are returned?
推荐答案
您可以尝试:
$search_word_fix=strtolower(str_replace(" ","%",$search_word_new));
$sql=mysql_query("SELECT * FROM tweets WHERE lower(content) LIKE '%$search_word_fix%' ORDER BY votes DESC LIMIT 20");
- 我添加了
strtolower
$search_word_fix
所有小写字母. - 在where子句中,我进行了更改
content
到lower(content)
我比较小写的content
. - I've added
strtolower
to make$search_word_fix
all lower case. - And in the where clause I've changed
content
tolower(content)
so that I compare with lowercase ofcontent
.
您可能已经按照其他答案中的建议在查询中进行了这两项更改.
You could have made both these changes in the query as suggested in other answer.
这篇关于PHP/Mysql搜索-区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!