简体   繁体   English

PHP/Mysql 搜索 - 区分大小写

[英]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");
  • I've added strtolower to make $search_word_fix all lower case.我添加了strtolower以使$search_word_fix全部小写。
  • And in the where clause I've changed content to lower(content) so that I compare with lowercase of content .在 where 子句中,我将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 数据库时选择最佳排序规则。

  • utf8_unicode_ci is useful if you want to sort for example german character set accurately but it's slow. utf8_unicode_ci 如果您想准确地对例如德语字符集进行排序但速度很慢,则很有用。
  • utf8_general_ci is by default the standard when creating MySQL database character set utf8 and it's the fastest, but not case sensitive. utf8_general_ci 默认是创建 MySQL 数据库字符集 utf8 时的标准,它是最快的,但不区分大小写。
  • Rule of thumb: Use always utf8_general_ci on MySQL and collate utf8_bin if case sensitive required OR use SELECT BINARY in your statement.经验法则:在 MySQL 上始终使用 utf8_general_ci 并在需要区分大小写的情况下整理 utf8_bin 或在您的语句中使用 SELECT BINARY。

请注意,如果您使用另一种语言并且您正在使用“strtolower()”,它不会将特殊字符(非英文字母)转换为小写,这将完成工作 mb_strtolower($text, 'utf8');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM