Illegal mix of collations for operation 'like' while searching with Ignited-Datatables

本文探讨了在使用Ignited-Datatables进行数据库搜索时遇到的MySQL非法混合字符集错误,特别是在输入非拉丁字符时出现的问题。文章提供了几种解决方案,包括避免在LIKE操作中使用DATETIME字段、调整字段类型和使用特定的字符集比较。

Join Stack Overflow to learn, share knowledge, and build your career.

I have successfully implemented Ignited-Datatables. However, while searching with database when typing "non-latin" characters like "İ,ş,ğ,.."

POST http://vproject.dev/module/user/ign_listing 500 (Internal Server Error)

Details are:

Illegal mix of collations for operation 'like' while searching
... (u.id_user LIKE '%İ%' OR u.first_name LIKE '%İ%' OR u.last_name LIKE '%İ%' OR ue.email LIKE '%İ%' OR u.last_login LIKE '%İ%' ) ...

%İ% part changes according to the non-latin character you typed.

Any idea for solving this?

share improve this question
 
   
One option is to use convert(datefield using utf-8)... – bbonev Jun 6 at 7:47 

3 Answers

up vote 13down voteaccepted

I figured out the problem. It seems it is DATETIME fields that causes the problem.

.. ue.last_login '%ayşenur%' 

gives error for Illegal mix of collations for operation 'like'. When I remove LIKE partials DATETIME fields, there are no error any more. I hope this helps.

share improve this answer
 
1 
Merhaba. did you fix this issue without removing DATETIME? – Rashad Sep 4 '14 at 8:23
3 
No, LIKE and DATETIME incompitablity is a MySQL problem. Hope to be fixed in later versions. – YahyaESep 4 '14 at 11:37
   
As I understand, the problem is on JSON UNICODE DECODES. So, in PHP 5.4 it should work. Did you try it at the latest PHP version? – Rashad Sep 5 '14 at 11:42
   
@RiKo You may be right. The question is one year old, so I did not try with the latest version. Let me and community know if you try and make it work. Thanks – YahyaE Sep 6 '14 at 8:33
   
@RiKo Did you try with the latest version? – YahyaE Oct 6 '14 at 17:38

Try the following:

u.id_user LIKE '%İ%' OR ... OR ... '%İ%' COLLATE utf8_bin

Refer to MySQL Unicode Character Sets

Also you can refer to MySQL _bin and binary Collations for more information on utf8_bin:

Nonbinary strings (as stored in the CHAR, VARCHAR, and TEXT data types) have a character set and collation. A given character set can have several collations, each of which defines a particular sorting and comparison order for the characters in the set. One of these is the binary collation for the character set, indicated by a _bin suffix in the collation name. For example, latin1 and utf8 have binary collations named latin1_bin and utf8_bin.

share improve this answer
 
   
Thanks for the knowledge but it didn't work. I think it is something about how the library handles the post data.– YahyaE Sep 5 '13 at 8:18
   
Why don't you try var_dump($this->input->post()); in your controller to see exactly what CI is doing to those chars. – doitlikejustin Sep 5 '13 at 14:57
   
I did. It is where the problem is. I am stacked with this, how come post is not utf-8? – YahyaE Sep 6 '13 at 3:16
   
The POST seems UTF-8, it is okay. Something wrong with Ignited-Datatables_ – YahyaE Sep 6 '13 at 5:03
   
Further reference [link] (github.com/IgnitedDatatables/Ignited-Datatables/issues/38) – YahyaE Sep 6 '13 at 5:05

i know that this is far too late, but, here my workaround.

SELECT * FROM (SELECT DATE_FORMAT(some_date,'%d/%m/%Y') AS some_date FROM some_table)tb1
WHERE some_date LIKE '% $some_variable %'

datetime/date column gives error for Illegal mix of collations for operation 'like', therefore, by converting it, as another table entity, previous column type will be replace with varchar type.

also, make sure to convert any column before convert it to temporary table, to make matching process more easier.

share improve this answer
 
   
Not in a position to test it but I liked the idea. – YahyaE Aug 18 '16 at 21:12

Your Answer

 
 
 
 
 

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged     or ask your own question.

 
Stack Overflow requires external JavaScript from another domain, which is blocked or failed to load.

 

转载于:https://www.cnblogs.com/SofuBlue/p/8079101.html

### 关于 SQLi-Labs 中 'Illegal mix of collations for operation UNION' 的解决方案 当在 SQLi-Labs 或其他环境中遇到 `Illegal mix of collations for operation 'UNION'` 错误时,这通常是因为参与 `UNION` 查询的列具有不同的字符集或排序规则 (collation),而 MySQL 不允许这种不一致的操作[^1]。 #### 原因分析 MySQL 数据库中的表和字段可以有不同的字符编码(如 utf8mb4 和 latin1),以及对应的排序规则(如 utf8_general_ci 和 utf8_bin)。如果两个查询的结果集中有相同位置上的列使用了不同字符集或排序规则,则会触发此错误。具体来说: - 字符串类型的列(如 VARCHAR, TEXT)必须拥有相同的字符集和排序规则才能执行 `UNION` 操作。 - 如果存在差异,可以通过显式转换来统一这些属性[^2]。 #### 解决方法 以下是几种常见的解决方式: 1. **强制指定字符集** 可以通过 `CAST()` 函数或者 `CONVERT()` 将涉及的字符串列转为同一字符集及排序规则。例如: ```sql SELECT CAST(column_name AS CHAR CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci) FROM table1 UNION SELECT column_name FROM table2; ``` 2. **修改数据库结构** 长期来看,调整相关表的设计使其保持一致性可能是更好的办法。比如运行如下语句更改整个表默认使用的字符集及其排序规则: ```sql ALTER TABLE your_table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; ``` 3. **临时处理数据源** 若无法改变实际存储的数据定义,在每次查询前先做必要的格式化也是可行的选择之一。即只针对当前请求动态修正而不影响原始记录的状态。 以上三种途径各有优劣需视具体情况选用最合适的方案实施修复工作[^3]。 ```sql -- Example using CONVERT function to align character sets during a union query. SELECT CONVERT(column_name USING utf8mb4), other_columns... FROM first_table WHERE conditions... UNION ALL SELECT CONVERT(column_name USING utf8mb4), other_columns... FROM second_table WHERE conditions... ; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值