简体   繁体   中英

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

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

I have 3 products:

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. The same goes for banana $ExistingWord1 which is Banana and should also search for 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.

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

For example :

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

Will match both Squirl, SQUIRL or squirl

In mysql char, varchar and text case insensitive http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html

Make sure that your text doesn't contains characters from other language

Mysql is case insensitive by default.

You probably have a case sensitive charset or your column has BINARY type assigned to it.

With BINARY a and A are different, which is not what you want ofc.

reference: this SO post .

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 ;)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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