简体   繁体   English

PHP MySQL区分大小写

[英]Php MySQL case sensitive

I know this question has been asked before. 我知道这个问题曾经被问过。 But nothing seems to work for me. 但是对我来说似乎没有任何作用。

So I have this code below. 所以我下面有这段代码。 What I am trying to do is Search in a database for table product_description and get the column name (this part so far works).The column name's collation is utf8_general_ci 我想做的是在数据库中搜索表product_description并获取列name (到目前为止这部分工作)。列名的排序规则是utf8_general_ci

   $SearchWord1 = "Squirl";
   $ExistingWord1 = "Banana";
   $ReplacerWord1 = "Orange";

I have 3 products: 我有3个产品:

name: SQUIRL banana pie
name: Squirl BANANA pie
name: SQUIRL banana pie

I want to search for Squirl,the search should grab both Squirl and SQUIRL. 我想搜索Squirl,搜索应同时包含Squirl和SQUIRL。 The same goes for banana $ExistingWord1 which is Banana and should also search for BANANA 香蕉$ExistingWord1也是如此,它是香蕉,也应该搜索BANANA

    $sql1 = "UPDATE product_description SET name = REPLACE(name, '$ExistingWord1', '$ReplacerWord1') WHERE name LIKE '%$SearchWord1%';";

if ($conn->query($sql1) === TRUE) {
    echo "<h1>Everything worked as planned</h1>";
} else {
    echo "Error updating record: " . $conn->error;
}

I read several posts on stackoverflow and nothing helped me thus far. 我读了几篇关于stackoverflow的文章,到目前为止没有任何帮助。

I don't know if it's me but I'm not sure to understand your question. 我不知道是不是我,但我不确定您的问题。

If all you want is to be sure to find the items you want regardless of the case, then you can probably use MySQL functions named UPPER(...) and LOWER(...) that allow you to convert a string in uppercase or lowercase respectively 如果只想确保无论大小写都能找到想要的项目,则可以使用名为UPPER(...)和LOWER(...)的MySQL函数,这些函数允许您将字符串转换为大写或分别小写

For example : 例如 :

SELECT name FROM product_description WHERE LOWER(name) = 'squirl';

Will match both Squirl, SQUIRL or squirl 将同时匹配Squirl,SQUIRL或Squirl

In mysql char, varchar and text case insensitive http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html 在mysql char,varchar和文本中,不区分大小写http://dev.mysql.com/doc/refman/5.0/en/case-sensitiveivity.html

Make sure that your text doesn't contains characters from other language 确保您的文字不包含其他语言的字符

Mysql is case insensitive by default. 默认情况下,MySQL不区分大小写。

You probably have a case sensitive charset or your column has BINARY type assigned to it. 您可能具有区分大小写的字符集,或者您的列已分配了BINARY类型。

With BINARY a and A are different, which is not what you want ofc. BINARY a和A不同,这不是您想要的c。

reference: this SO post . 参考: 本SO帖子

Be careful tho - as CI tables could give you duplicate results when products / items have the same name and you don't fetch them by ID ;) 请当心-如果产品/商品具有相同的名称并且您没有通过ID提取它们,则CI表可能会给您重复的结果;)

通常,在MySQL中,基于字符串的搜索不区分大小写。

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

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