简体   繁体   中英

PDO MySQL (MariaDB) query with UTF8 column is not case insensitive though default collation is used

I have a query like this with a UTF8 name:

select column from table where name = 'something'

When I try this from my mysql client (HeidiSQL) then the result is returned even if there is a case difference between name and something. This is good.

Howevever, when I run the same query from PHP with PDO then the query is case-sensitive for some reason.

The table uses the default collation utf8_general_ci and in the PDO connection I only set charset=utf8.

Shouldn't then the default collation for the PDO connection also be case insensitive?

From PHP I printed the collation variables:

show variables where variable_name like '%coll%';

and it said:

array(3) {
  [0]=>
  array(2) {
    ["Variable_name"]=>
    string(20) "collation_connection"
    ["Value"]=>
    string(15) "utf8_general_ci"
  }
  [1]=>
  array(2) {
    ["Variable_name"]=>
    string(18) "collation_database"
    ["Value"]=>
    string(15) "utf8_general_ci"
  }
  [2]=>
  array(2) {
    ["Variable_name"]=>
    string(16) "collation_server"
    ["Value"]=>
    string(15) "latin1_swedish_ci"
  }
}

I'm not sure why the server collation latin1_swedish_ci caused a problem when the connection and database collations were utf8_general_ci, but when I changed the server collation too to utf8_general_ci:

set global collation_server = "utf8_general_ci";

Then the query started working case insensitively from PHP too.

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