[英]PHP/Mysql Search - Case sensitive
Im using the following PHP and MySql to fetch rows from a table,我使用以下 PHP 和 MySql 从表中获取行,
$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");
The 'content' field is a TEXT field containing tweets. “内容”字段是包含推文的 TEXT 字段。
The problem I have is if I search ' S tackoverflow' I get all the results containing 'Stackoverflow' but no results when the text is ' s tackoverflow'.我的问题是,如果我搜索作者:tackoverflow“我得到的所有包含“#1”的结果,但没有结果时,文字的tackoverflow”。 Basically the search is case sensitive.
基本上搜索是区分大小写的。
Is it possible to change the query or PHP so when searching for 'Stackoverflow' both upper and lower case results are returned?是否可以更改查询或 PHP,以便在搜索“Stackoverflow”时同时返回大写和小写结果?
You can try:你可以试试:
$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
to make $search_word_fix
all lower case.strtolower
以使$search_word_fix
全部小写。content
to lower(content)
so that I compare with lowercase of content
.content
更改为lower(content)
以便与content
小写进行比较。 You could have made both these changes in the query as suggested in other answer.您可以按照其他答案中的建议在查询中进行这两项更改。
Force the cases of both the search term and the column value:强制搜索词和列值的大小写:
SELECT * FROM tweets WHERE LOWER(content) LIKE LOWER('%$search_word_fix%') ORDER BY votes DESC LIMIT 20
or:或:
SELECT * FROM tweets WHERE UPPER(content) LIKE UPPER('%$search_word_fix%') ORDER BY votes DESC LIMIT 20
http://dev.mysql.com/doc/refman/5.0/en/string-functions.htmlhttp://dev.mysql.com/doc/refman/5.0/en/string-functions.html
The 'proper' way to do it is to set it to case-insensitive collation:执行此操作的“正确”方法是将其设置为不区分大小写的排序规则:
CREATE TABLE foo (col1 varchar(24) COLLATE utf8_bin,col2 varchar(24) COLLATE utf8_general_ci);
Query OK, 0 rows affected (0.03 sec)
DB 5.1.49-1-log:test mysql> INSERT INTO foo VALUES ('stackoverflow','stackoverflow');
Query OK, 1 row affected (0.01 sec)
DB 5.1.49-1-log:test mysql> SELECT * FROM foo WHERE col1 LIKE 'Stackoverflow';
Empty set (0.00 sec)
DB 5.1.49-1-log:test mysql> SELECT * FROM foo WHERE col2 LIKE 'Stackoverflow';
+---------------+---------------+
| col1 | col2 |
+---------------+---------------+
| stackoverflow | stackoverflow |
+---------------+---------------+
1 row in set (0.00 sec)
DB 5.1.49-1-log:test mysql> SELECT * FROM foo WHERE col1 COLLATE utf8_general_ci LIKE 'Stackoverflow';
+---------------+---------------+
| col1 | col2 |
+---------------+---------------+
| stackoverflow | stackoverflow |
+---------------+---------------+
1 row in set (0.00 sec)
Change the COLLATION
of the column in question ( content
) to be case insensitive, such as utf8mb4_unicode_ci
.将相关列 (
content
) 的COLLATION
更改为不区分大小写,例如utf8mb4_unicode_ci
。
Doing the case folding in PHP is costly and inefficient.在 PHP 中进行大小写折叠既昂贵又低效。
mysql> SELECT * FROM myDb.myTable WHERE username = 'test980'; mysql> SELECT * FROM myDb.myTable WHERE username = 'test980'; 1 row in set (0.00 sec)
1 行(0.00 秒)
mysql> SELECT * FROM myDb.myTable WHERE username = 'TEST980'; mysql> SELECT * FROM myDb.myTable WHERE username = 'TEST980'; Empty set (0.00 sec)
空集(0.00 秒)
MySQL columns can be made case-sensitive by creating them with the binary keyword.可以通过使用 binary 关键字创建 MySQL 列来区分大小写。 I suspect this is your problem.
我怀疑这是你的问题。 You can modify the column not to be binary or change your query to:
您可以将列修改为非二进制或将查询更改为:
SELECT * FROM myDb.myTable WHERE UCASE(username) = 'TEST980'; SELECT * FROM myDb.myTable WHERE UCASE(username) = 'TEST980';
which effectively makes string comparisons case insensitive despite the Binary character set chosen.尽管选择了二进制字符集,但它有效地使字符串比较不区分大小写。
It's all about choosing the best collation when creating MySql database.在创建 MySql 数据库时选择最佳排序规则。
请注意,如果您使用另一种语言并且您正在使用“strtolower()”,它不会将特殊字符(非英文字母)转换为小写,这将完成工作 mb_strtolower($text, 'utf8');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.